Jump to content

Do you think this is achievable


RuleBritannia

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)) {
Link to comment
Share on other sites

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 by RuleBritannia
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.