2

I think I need a regular expression that uses a look behind. I want the regular expression to match the 'this' in a string as long as it doesn't come after '//'

So for example it would match the 'this' if this was the string:

I'm using this as my example

but not if it was:

// I'm using this as my example

8
  • 2
    What have you tried? Commented Feb 12, 2012 at 20:14
  • Do you want to parse commented-out code? Commented Feb 12, 2012 at 20:16
  • //(?=this) would match the // that was followed by this regular-expressions.info/lookaround.html Commented Feb 12, 2012 at 20:20
  • @BartKiers (?!//) I've tried doing that before, but only works on what's directly next to it. I've never used lookbehinds before so sorry if that's completely wrong. Commented Feb 12, 2012 at 20:28
  • @DamienPirsy Yeah that's the plan Commented Feb 12, 2012 at 20:28

2 Answers 2

2

This regular expression should do it:

^([^/]|/(?!/))*this

The ([^/]|/(?!/))* matches any sequence that does not contain a //.

Sign up to request clarification or add additional context in comments.

Comments

1

This is one of many situations where lookbehind seems like the obvious tool for the job, but you can't use it. The main problem is that a lookbehind subexpression can't use quantifiers like ?, *, +, or {min,max}, so this won't work:

preg_match('~(?<!//.*)\bthis\b~', $subject, $match);  // nope!  

In most regex flavors you would have to match the whole line with a regex that doesn't allow // to appear anywhere before the token you're looking for. @Gumbo showed one way to do that, and here's another:

preg_match('~^(?:(?!//).)*\bthis\b~', $subject, $match); 

If your goal is to replace the token with another--for example, replacing this with that--you'll have to capture the stuff before it and plug it back in:

preg_replace('~^((?:(?!//).)*)\bthis\b~', '$1that', $subject);

However, PHP offers another option: \K, Match Point Reset operator (or as I like to call it, "Poor Man's Lookbehind").

preg_match('~^(?:(?!//).)*\K\bthis\b~', $subject, $match); 

You match the preceding matter in the usual way, but the \K tells the regex engine to pretend the match really started just before the \bthis\b, not at the beginning of the string, so the value of $match[0] is simply "this". If you're doing a replace, there's no need to capture the preceding matter and plug it back in:

preg_replace('~^(?:(?!//).)*\K\bthis\b~', 'that', $subject);

\K originated in the Perl regex flavor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.