- 论坛徽章:
- 0
|
ruby 的回溯 正则表达式
matchesrubyregular expression
Ruby代码- ^ - Matches beginning of line
- $ - Matches end of line
- \A - Matches beginning of string.
- \Z - Matches end of string. If string ends with a newline, it matches just before newline
- \z - Matches end of string
- \G - Matches point where last match finished
- \b - Matches word boundaries when outside brackets; backspace (0x08) when inside brackets
- \B - Matches non-word boundaries
- (?=pat) - Positive lookahead assertion: ensures that the following characters match pat, but doesn't include those characters in the matched text
- (?!pat) - Negative lookahead assertion: ensures that the following characters do not match pat, but doesn't include those characters in the matched text
- (?<=pat) - Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text
- (?<!pat) - Negative lookbehind assertion: ensures that the preceding characters do not match pat, but doesn't include those characters in the matched text
- # If a pattern isn't anchored it can begin at any point in the string
-
- /real/.match("surrealist") #=> #<MatchData "real">
- # Anchoring the pattern to the beginning of the string forces the
- # match to start there. 'real' doesn't occur at the beginning of the
- # string, so now the match fails
- /\Areal/.match("surrealist") #=> nil
- # The match below fails because although 'Demand' contains 'and', the
- pattern does not occur at a word boundary.
- /\band/.match("Demand")
- # Whereas in the following example 'and' has been anchored to a
- # non-word boundary so instead of matching the first 'and' it matches
- # from the fourth letter of 'demand' instead
- /\Band.+/.match("Supply and demand curve") #=> #<MatchData "and curve">
- # The pattern below uses positive lookahead and positive lookbehind to
- # match text appearing in <b></b> tags without including the tags in the
- # match
- /(?<=<b>)\w+(?=<\/b>)/.match("Fortune favours the <b>bold</b>")
- #=> #<MatchData "bold">
- ^ - Matches beginning of line
- $ - Matches end of line
- \A - Matches beginning of string.
- \Z - Matches end of string. If string ends with a newline, it matches just before newline
- \z - Matches end of string
- \G - Matches point where last match finished
- \b - Matches word boundaries when outside brackets; backspace (0x08) when inside brackets
- \B - Matches non-word boundaries
- (?=pat) - Positive lookahead assertion: ensures that the following characters match pat, but doesn't include those characters in the matched text
- (?!pat) - Negative lookahead assertion: ensures that the following characters do not match pat, but doesn't include those characters in the matched text
- (?<=pat) - Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text
- (?<!pat) - Negative lookbehind assertion: ensures that the preceding characters do not match pat, but doesn't include those characters in the matched text
- # If a pattern isn't anchored it can begin at any point in the string
- /real/.match("surrealist") #=> #<MatchData "real">
- # Anchoring the pattern to the beginning of the string forces the
- # match to start there. 'real' doesn't occur at the beginning of the
- # string, so now the match fails
- /\Areal/.match("surrealist") #=> nil
- # The match below fails because although 'Demand' contains 'and', the
- pattern does not occur at a word boundary.
- /\band/.match("Demand")
- # Whereas in the following example 'and' has been anchored to a
- # non-word boundary so instead of matching the first 'and' it matches
- # from the fourth letter of 'demand' instead
- /\Band.+/.match("Supply and demand curve") #=> #<MatchData "and curve">
- # The pattern below uses positive lookahead and positive lookbehind to
- # match text appearing in <b></b> tags without including the tags in the
- # match
- /(?<=<b>)\w+(?=<\/b>)/.match("Fortune favours the <b>bold</b>")
- #=> #<MatchData "bold">
复制代码 |
|