john_bboy7 Posted April 19, 2009 Share Posted April 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154726-solved-a-wierd-regex-problem/ Share on other sites More sharing options...
Rithiur Posted April 19, 2009 Share Posted April 19, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/154726-solved-a-wierd-regex-problem/#findComment-813644 Share on other sites More sharing options...
john_bboy7 Posted April 19, 2009 Author Share Posted April 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/154726-solved-a-wierd-regex-problem/#findComment-813651 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.