RuleBritannia Posted November 5, 2013 Share Posted November 5, 2013 So I currenty have a working regex for my needs. However a problem has occured. (page consisting of lots of strings) $s1 = 'this is the other version new'; $s2 = 'this is the other version old'; $look = 'new'; My regex currently matches most of the string, followed by the look variable, so it matches the $s1 without going to the lengths of rewriting the regex(its actually more complex than this, im just trying to simplify for whoever reads this) is there a way to provide the variable $look, with a string, that is processed by the regex as NOT find this string. A non-working theoretical example $look = [^n][^e][^w] my wishfull returned match would this time around be $s2 Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/283603-do-you-think-this-is-achievable/ Share on other sites More sharing options...
requinix Posted November 5, 2013 Share Posted November 5, 2013 If the problem is as simple as you've described (hint hint) then strpos to find the string and only accept if it isn't found. So how about the full story? And full regex. Quote Link to comment https://forums.phpfreaks.com/topic/283603-do-you-think-this-is-achievable/#findComment-1456996 Share on other sites More sharing options...
RuleBritannia Posted November 5, 2013 Author Share Posted November 5, 2013 If the problem is as simple as you've described (hint hint) then strpos to find the string and only accept if it isn't found. So how about the full story? And full regex. But using strpos and if would mean pasteing the regex twice with just slightly edited parameters?, This method is easily do-able and can solve the problem right now, But I was just curios if there was a better way of doing it. I can post the full regex/story if needed, but id have to prepare quite alot of data, which is why I didnt already do it. Quote Link to comment https://forums.phpfreaks.com/topic/283603-do-you-think-this-is-achievable/#findComment-1457001 Share on other sites More sharing options...
requinix Posted November 5, 2013 Share Posted November 5, 2013 You wouldn't use regular expressions with strpos. All you posted was the value "new" which certainly doesn't need a regex. The idea I was trying to get across was just doing a normal match and then negating the result. If if (preg_match($regex, $input)) {is the opposite of what you want, then what you want is if (!preg_match($regex, $input)) { Quote Link to comment https://forums.phpfreaks.com/topic/283603-do-you-think-this-is-achievable/#findComment-1457005 Share on other sites More sharing options...
RuleBritannia Posted November 5, 2013 Author Share Posted November 5, 2013 (edited) The string is actually a link, example http://000000000.this-is-another-version-new.com OR http://111111111.this-is-another-version-old.com So strpos would just result in a link that doesnt work.(more factors than just the old|new in the overall match) The reason this would have to be done within the regex is so that it sees both matches within the whole string, but chooses correctly which string to return(based on the look variable) I will post the full story/regex soon, just doing another task right now. Thanks for your time Edited November 5, 2013 by RuleBritannia Quote Link to comment https://forums.phpfreaks.com/topic/283603-do-you-think-this-is-achievable/#findComment-1457007 Share on other sites More sharing options...
.josh Posted November 5, 2013 Share Posted November 5, 2013 without going to the lengths of rewriting the regex(its actually more complex than this, im just trying to simplify for whoever reads this) This, almost without fail, leads to wasting both your time and our time. Especially when it comes to regex. What may work in the "simplified" scenario may not work in the bigger picture. Always explain the full, actual problem. But in any case, it sounds like what you may be looking for is a negative lookahead. For example: $s1 = 'this is the other version new'; $s2 = 'this is the other version old'; preg_match('~(?!.*new)this is the other version.*~',$s1,$m); // output: no match preg_match('~(?!.*new)this is the other version.*~',$s2,$m); /* output: Array ( [0] => this is the other version old ) */ (?!pattern) is the syntax for a negative lookahead. The important thing to understand is that it is a zero-width assertion. This means that when something matches, the engine doesn't actually consume what it matches; it doesn't move the pointer forward. So nothing it matches will show in the returned $m array. Hence the name lookahead. It's like if you are walking in the woods and you have a walking stick and you poke at the ground ahead of you to make sure there aren't any snakes before you take another step forward - poking the ground in front of you gives you insight on whether or not you should move forward, but it doesn't itself move you forward. (?!.*new) uses a basic .* match all to cover the whole string and new looks for the literal substring "new". So this is saying "look ahead in this string to see if 'new' is NOT in there somewhere - IOW "see if this pattern does NOT match - and if true (it does not match), proceed with the rest of the pattern. Then the rest of the pattern is pretty basic: match the literal phrase followed by a .* to match the " old" after it. Quote Link to comment https://forums.phpfreaks.com/topic/283603-do-you-think-this-is-achievable/#findComment-1457069 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.