Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What meaning does it have when I put a space within a range quantifier in a regex?

Putting a space within a range quantifier in a regex seems syntactically valid:

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

However, such space seems to alter the behaviour compared to when there is no such space:

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

What would be the meaning of a regex with space within a range like /.{1, 2}/?

like image 250
sawa Avatar asked Jan 13 '16 12:01

sawa


1 Answers

/.{1, 2}/ 

matches "a{1, 2}". Though it is syntactically valid, the {1, 2} stops being a limiting quantifier.

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

like image 195
vks Avatar answered Nov 10 '22 18:11

vks