Hjälp:Regex

Från Rilpedia

(Skillnad mellan versioner)
Hoppa till: navigering, sök
(Positive and Negative '''Lookbehind''')
 
(4 mellanliggande versioner visas inte.)
Rad 6: Rad 6:
;Negative lookahead: Match something '''not''' followed by something else.
;Negative lookahead: Match something '''not''' followed by something else.
-
:;Example:<code> A '''q''' that '''is ''not''''' followed by a '''u''': q(?!'''u''')</code>".
+
:;'''Example: A <code>q</code> that is ''not'' followed by a <code>u</code>:'''
-
:(inside the lookahead we have the trivial regex "u").
+
::<code>q(?!'''u''')</code>.
 +
:(inside the lookahead we have the trivial regex "<code>u</code>").
;Positive lookahead: Match something '''not''' followed by something else.  
;Positive lookahead: Match something '''not''' followed by something else.  
-
:;Example:<code>A '''q''' that ''is'' followed by a '''u''' "q(?=u)"</code>
+
:;'''Example: A <code>q</code> that ''is'' followed by a <code>u</code>:'''
-
:''(without making the u part of the match.
+
::<code>q(?=u)</code>
-
 
+
:(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))".
+
 +
;Set a variable with the match-result: Put capturing parentheses around the regex inside the lookahead, like this: "<code>(?=(regex))</code>".
== Positive and Negative '''Lookbehind''' ==
== Positive and Negative '''Lookbehind''' ==
-
;Positive lookbehind: (?<=text)x - Match something which '''is''' preceded by something else.
+
;Positive lookbehind: <code>(?<=text)x</code> - Match something which '''is''' preceded by something else.
-
:;Example: Match only a '''b''' which is preceeded by an '''a'''.
+
:;Example: Match only a '''<code>b</code>''' which is preceeded by an '''<code>a</code>'''.
-
:''In "cab" the regex "(?<=a)b" matches the "b" (and only the "b"), but does not match "bed" or "debt".
+
:In "cab" the regex "<code>(?<=a)b</code>" matches the "<code>b</code>" (and only the "<code>b</code>"), but does not match "bed" or "debt".
-
;Negative lookbehind: (?<!text)x - Match something which is '''not''' preceded by something else.
+
;Negative lookbehind: <code>(?<!text)x</code> - Match something which is '''not''' preceded by something else.
-
:; Example: A "b" that is not preceded by an "a": <code>(?<!a)b</code>
+
:; Example: A "<code>b</code>" that is not preceded by an "a": <code>(?<!a)b</code>
-
:''It will not match "cab", but will match the "b" (and only the "b") in "bed" or "debt".
+
:It will not match "<code>cab</code>", but will match the "<code>b</code>" (and only the "<code>b</code>") in "<code>bed</code>" or "<code>debt</code>".
    
    
=== Important Notes About Lookbehind ===
=== 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 <code>"\b\w+(?<!s)\b"</code>.  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 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 "<code>s</code>", you could use "<code>\b\w+(?<!s)\b</code>".  This is definitely not the same as "<code>\b\w+[^s]\b</code>".  When applied to "John's", the former will match "<code>John</code>" and the latter "<code>John'</code>" (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.
'''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.

Nuvarande version från 15 april 2007 kl. 01.12

Ril_red.png
Rilpedia artikel
rpsv.header.diskuteraikon2.gif

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:
q(?!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:
q(?=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.
Example
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.
Example
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