Forward Reference In Regex
What is the difference of the following regular expressions? (\2amigo|(go!))+ (amigo|(go!))+ They both match the same strings. https://regexr.com/3u62t How does the forward refere
Solution 1:
The second "(amigo|(go!))+" captures: amigoamigo
The first "(\2amigo|(go!))+ " doesn't.
Solution 2:
The behavior depends on the language.
In Ruby and Perl forward references can also be used, but be sure the referenced parenthesis
has matched when is going to be used. This usually means that the forward reference
is inside some repetition group. For example, in Ruby this regexp matches with train
only if
it is prefixed by at least one choo
:
$ irb
irb(main):052:0> regex = /(\2train|(choo))+/
=> /(\2train|(choo))+/
irb(main):053:0> 'choochootrain' =~ regex
=> 0
irb(main):054:0> $&
=> "choochootrain"
irb(main):055:0> $1
=> "chootrain"
irb(main):056:0> $2
=> "choo"
irb(main):004:0> 'train' =~ regex
=> nil
This is not the case in JavaScript:
[~/.../github-actions/225-github-actions-demo(master)]$ node
Welcome to Node.js v13.5.0.
Type".help"for more information.
> regex = /(\2train|(choo))+//(\2train|(choo))+/
> regex.exec('train')
[
'train',
'train',
undefined,
index: 0,
input: 'train',
groups: undefined
]
In fact, it does match train
(The \2
is assumed empty):
Post a Comment for "Forward Reference In Regex"