What does it make sense when I put a space in the range quantifier in a regular expression?

Putting a space in the range quantifier in the regular expression seems syntactically valid:

/.{1, 2}/ # => /.{1, 2}/ 

However, such a space apparently changes behavior compared to the absence of such a space:

 "a" =~ /.{1,2}/ # => 0 "a" =~ /.{1, 2}/ # => nil 

What would be the meaning of a regular expression with space in a range such as /.{1, 2}/ ?

+7
ruby regex literals
source share
1 answer
 /.{1, 2}/ 

matches "a{1, 2}" . Although this is syntactically valid, {1, 2} ceases to be a bounding quantifier.

As soon as a space appears between the comma and the numeric value max , {1, 2} behaves like a literal string match.

+7
source share

All Articles