Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex: question mark followed by colon as an alternative

In rails cucumber there is this regex When /^(?:|I )go to (.+)$/ do |page_name|

I know ?: is a non-capturing group but what does it mean when it is there as an alternative separated by | ?

like image 753
ayathustring Avatar asked Mar 21 '23 17:03

ayathustring


1 Answers

This isn't a special group, it just means "match nothing or I": http://www.rubular.com/r/H3iJFLXaab

This should be the same as writing (?:I )?
(or to be more precise, (?:I )?? - because the empty string has precedence over I, see also Is the lazy version of the 'optional' quantifier ('??') ever useful in a regular expression? )

like image 145
Kobi Avatar answered Mar 31 '23 21:03

Kobi