NYCPHP Meetup

NYPHP.org

[nycphp-talk] Question about REGEX's...

Anirudh Zala arzala at gmail.com
Tue Nov 28 22:42:59 EST 2006


You are violating 2 rules here of how to use regular expressions.

#1 Regular expressions always needs 2 arguments. 1: Pattern to be matched and 
2: String in which defined pattern is to be matched.

Here you missed 2nd arguments that is why parser gives ) => / error.

#2 Quote (double or single) is almost must in regular expressions specially 
when you want to expand modifiers (?, ., + etc)

If you don't use quote then code will work but pattern will be treated as a 
constant and php will go to parse it and may generate "Notice" if such can 
not be found. Below is the correct way to use this regular expression 
function.

$trans_text=preg_match("/<div id=result_box dir=ltr>(.+?)<\/div>/",
$ss__strToBeLookedIn);

Also note "</div> => <\/div>", because php thinks that "/" is end of boundary 
which is not actually :)

Thanks

Zala

On Wednesday 29 November 2006 05:54, Anthony Papillion wrote:
> Hello Everyone,
>
> I am using the following regex to retrieve text between two <div> tags.
>
> $trans_text = preg_match(/<div id=result_box dir=ltr>(.+?)</div>/);
>
> However, the parser keeps telling me it expects a ) but found / instead.
> What am I doing wrong?
>
> Can anyone please help a regex newbie in his quest? lol
>
> Thanks,
> Anthony



More information about the talk mailing list