Jump to content

[SOLVED] A wierd regex problem


john_bboy7

Recommended Posts

i am matching an html document which should start from

 

<div class="postbody_div">

 

And end till

 

<span class="name"></span></td>

 

I can do that with:

 

<div class="postbody_div">([^*])+<span class="name"></span></td>[/Php]

 
But in that document there are more than one of these codes so it matches all of them.. then i used "?" in it.. as follow:
 
[code=php:0]<div class="postbody_div">([^*?])+<span class="name"></span></td>

 

So it should stop searching after the first match. But it shows nothing..  ???

And when i change:

 

<span class="name"></span>

 

to some other word or sentence which is 1 or two lines above then it matches again.. (<== thats not what i want) i want it to match with my first code. and stop at the first match.

Link to comment
https://forums.phpfreaks.com/topic/154726-solved-a-wierd-regex-problem/
Share on other sites

You have a bit of misunderstanding with the regular expression there. The contents inside the [ ] are the things that the character class matches (or since you have ^ at the beginning, what it doesn't match). Since you have [^*] in the first regexp, the character class matches anything but * character. If you use [^*?], it matches anything except * or ? characters.

 

If you want to make a quantifier ungreedy, you need to insert the ? after the quantifier. In you case, your regexp should probably have something like ([^*]+?) (and remove the + outside the parenthesis, assuming you want the entire match into that subpattern).

 

Also, the if you're using the ereg() functions, they do not support ungreedy modifiers. You should be using preg_ functions instead. Also, if you meant the [^*] character class to mean any character, you should probably be using dot (which matches any character, except newline unless dotall modifier is used) with dotall modifier 's' like:

 

preg_match('#<div class="postbody_div">(.+?)<span class="name"></span></td>#s', $string, $matches);

 

Oh, and if you meant to match any character with the [^*] character class, you might want to use dot instead like (.+?) since dot matches any character (except newlines, so you need to use the dotall modifier like 's' like /.+/s

You have a bit of misunderstanding with the regular expression there. The contents inside the [ ] are the things that the character class matches (or since you have ^ at the beginning, what it doesn't match). Since you have [^*] in the first regexp, the character class matches anything but * character. If you use [^*?], it matches anything except * or ? characters.

 

If you want to make a quantifier ungreedy, you need to insert the ? after the quantifier. In you case, your regexp should probably have something like ([^*]+?) (and remove the + outside the parenthesis, assuming you want the entire match into that subpattern).

 

Also, the if you're using the ereg() functions, they do not support ungreedy modifiers. You should be using preg_ functions instead. Also, if you meant the [^*] character class to mean any character, you should probably be using dot (which matches any character, except newline unless dotall modifier is used) with dotall modifier 's' like:

 

preg_match('#<div class="postbody_div">(.+?)<span class="name"></span></td>#s', $string, $matches);

 

Oh, and if you meant to match any character with the [^*] character class, you might want to use dot instead like (.+?) since dot matches any character (except newlines, so you need to use the dotall modifier like 's' like /.+/s

 

Man thats some real info!

Thanks it worked.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.