Regexp bug - max quantifier

regex for matching 1 to 4 caps alpha, followed by 1 to 4 digits seems to
fail for 5 alpha (tho not 5 digits)

see examples below, 1, 3 and 4 give expected response, but 2 does not.
Using “http://myregexp.com/” gives expected results (example 2 does not
match)

1

ruby -v -e ‘puts( /^[A-Z]{1,4}[0-9]{1,4}$/ =~ “ABCD1234” )’
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
0

2

ruby -v -e ‘puts( /^[A-Z]{1,4}[0-9]{1,4}$/ =~ “ABCDE1234” )’
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
1

3

ruby -v -e ‘puts( /^[A-Z]{1,4}[0-9]{1,4}$/ =~ “ABCD12345” )’
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]

4

ruby -v -e ‘puts( /^[A-Z]{1,4}[0-9]{1,4}$/ =~ “ABCDE12345” )’
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]

On Fri, Jan 20, 2012 at 7:52 PM, Straff W.
[email protected] wrote:

0

4

ruby -v -e ‘puts( /^[A-Z]{1,4}[0-9]{1,4}$/ =~ “ABCDE12345” )’
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]


Posted via http://www.ruby-forum.com/.

Hi Straff W.,

Regexp is ok on my setup (Ubuntu 11.10 amd-64 + RVM).

ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
ABCD1234 matches /^([A-Z]{1,4})([0-9]{1,4})$/ capturing: [“ABCD”,
“1234”]
ABCDE1234 doesn’t match /^([A-Z]{1,4})([0-9]{1,4})$/
ABCD12345 doesn’t match /^([A-Z]{1,4})([0-9]{1,4})$/
ABCDE12345 doesn’t match /^([A-Z]{1,4})([0-9]{1,4})$/

Made this gist for others to test it too.

https://gist.github.com/1650955

Abinoam Jr.

On Sat, Jan 21, 2012 at 12:08 AM, Abinoam Jr. [email protected] wrote:

On Fri, Jan 20, 2012 at 7:52 PM, Straff W.
[email protected] wrote:

2

ruby -v -e ‘puts( /^[A-Z]{1,4}[0-9]{1,4}$/ =~ “ABCDE1234” )’
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
1

(One thing more)…

If it’s returning 1 perhaps it is not respecting the anchor (^) for
begining and making the match against the second (1-indexed) character
(“BCDE”). (So, perhaps it’s not the “max quantifier” as on the post
subject).

without the anchor… ("^")

match = /[A-Z]{1,4}[0-9]{1,4}$/.match “ABCDE1234” => #<MatchData
“BCDE1234”>

Could you please run the script on the gist to see what it is
capturing? ( https://gist.github.com/1650955 )

Abinoam Jr.

I guess, you are using ruby on Windows OS and cmd.exe.(from
[i386-mingw32])
It has differ from UNIX shell family.
This probrem looks from quotaions.

base quotation change ’ to “” on cmd.exe

ruby -v -e “puts( /^[A-Z]{1,4}[0-9]{1,4}$/ =~ ‘ABCD1234’ )”
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
0

ruby -v -e “puts( /^[A-Z]{1,4}[0-9]{1,4}$/ =~ ‘ABCDE123’ )”
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]

ruby -v -e “puts( /^[A-Z]{1,4}[0-9]{1,4}$/ =~ ‘ABCD12345’ )”
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]

ruby -v -e “puts( /^[A-Z]{1,4}[0-9]{1,4}$/ =~ ‘ABCDE12345’ )”
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]

2012/1/21 Straff W. [email protected]:

yes, it is Windows, and yes, swapping quotes around (double for the -e
command part, and single for the string to be matched) produced the
expected result (as per kachick)

further, irb gives the expected results regardless of using single or
double quotes around the string to be matched against (ABCD1234)

thanks for your help

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs