Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.