You are here: Re: Regex question, Text in brackets? « All PHP « IT news, forums, messages
Re: Regex question, Text in brackets?

Posted by Rik on 10/09/68 11:56

FFMG wrote:
> Hi,
>
> I am looking for a word inside a bracket, the text is something like.
> LINE1( A=10) some more text
> LINE2( B=12) yet some more text
> LINE3( A=13) and some more text
> LINE4( B=14) and some more text
>
> If I am looking for the value of 'A' in LINE1 then I would probably
> do something like
> /LINE1.*?\(.*?A=([^\)]).*?)/is
>
> But if I am looking for a value that is not in the bracket but found
> in another then it will return the value.
> For example
> /LINE1.*?\(.*?B=([^\)]).*?)/is
>
> Would give me $1 = 12 even though B is in the next set of brackets.
> How can I return the values found in the correct set of brackets? (and
> nothing if it is not found).

Well, key is you have to stay _inside_ the brackets. Be very wary with the
dot here, it will match newlines due to /s, and skip over brackets if 'B='
is not in them.

As I see it, you've got 2 options:

1. Don't let the dot match newlines, i.e:
/LINE1.*?\(.*?B=([^\)]+)\)/i

2. Stay inside the first found brackets, i.e:
/LINE1[^\(]*\([^\)]*?B=([^\)]+)\)/i


Offcourse, if, like in your example, the value of A or B is always an
integer, better to match that:
/LINE1[^\(]*\([^\)]*?B=[^0-9\)]*([0-9]+)\)/i

--
Grtz,

Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация