Från Rilpedia

Hoppa till: navigering, sök
Rilpedia artikel

Regex - for advanced text processing - with the use of Regular Expressions. Below a compressed version of text from more complete Regex references.

Positive and Negative Lookahead

Negative lookahead
Match something not followed by something else.
Example: A q that is not followed by a u:
(inside the lookahead we have the trivial regex "u").
Positive lookahead
Match something not followed by something else.
Example: A q that is followed by a u:
(without making the u part of the match.)
Set a variable with the match-result
Put capturing parentheses around the regex inside the lookahead, like this: "(?=(regex))".

Positive and Negative Lookbehind

Positive lookbehind
(?<=text)x - Match something which is preceded by something else.
Match only a b which is preceeded by an a.
In "cab" the regex "(?<=a)b" matches the "b" (and only the "b"), but does not match "bed" or "debt".
Negative lookbehind
(?<!text)x - Match something which is not preceded by something else.
A "b" that is not preceded by an "a": (?<!a)b
It will not match "cab", but will match the "b" (and only the "b") in "bed" or "debt".

Important Notes About Lookbehind

The good news is that you can use lookbehind anywhere in the regex, not only at the start. If you want to find a word not ending with an "s", you could use "\b\w+(?<!s)\b". This is definitely not the same as "\b\w+[^s]\b". When applied to "John's", the former will match "John" and the latter "John'" (including the apostrophe).

The bad news is that most regex flavors do not allow you to use just any regex inside a lookbehind, because they cannot apply a regular expression backwards. Therefore, the regular expression engine needs to be able to figure out how many steps to step back before checking the lookbehind.

Therefore, only fixed-length strings are allowed. This means you can use literal text and character classes. You cannot use repetition or optional items. You can use alternation, but only if all options in the alternation have the same length.

The only regex flavors that allow you to use a full regular expression inside lookbehind are RegexBuddy (2.0.3 and later), PowerGREP (3.0.0 and later), and the .NET framework (all versions).

Personliga verktyg