Why is this regex invalid?

D

Daniel Finnie

irb(main):002:0> regex = /([0-9]*)([^\+- ]+)(.*)/
SyntaxError: compile error
(irb):2: invalid regular expression: /([0-9]*)([^\+- ]+)(.*)/
from (irb):2

irb(main):003:0> regex = /([0-9]*)([^\+ -]+)(.*)/
=> /([0-9]*)([^\+ -]+)(.*)/

The only thing that it different in the 2 regexps is the placement of
the space in the square brackets, yet the first regexp is invalid and
the 2nd valid.

Why is this?

Thanks,
Dan
 
M

MenTaLguY

irb(main):002:0> regex = /([0-9]*)([^\+- ]+)(.*)/
SyntaxError: compile error
(irb):2: invalid regular expression: /([0-9]*)([^\+- ]+)(.*)/
from (irb):2

irb(main):003:0> regex = /([0-9]*)([^\+ -]+)(.*)/
=> /([0-9]*)([^\+ -]+)(.*)/

The only thing that it different in the 2 regexps is the placement of
the space in the square brackets, yet the first regexp is invalid and
the 2nd valid.

Why is this?

When an unescaped - appears in any position but the first or final one inside brackets, it is interpreted as a range separator rather than a literal '-'. Apparently '\+- ' isn't a valid range.

-mental
 
A

Alex LeDonne

irb(main):002:0> regex = /([0-9]*)([^\+- ]+)(.*)/
SyntaxError: compile error
(irb):2: invalid regular expression: /([0-9]*)([^\+- ]+)(.*)/
from (irb):2

irb(main):003:0> regex = /([0-9]*)([^\+ -]+)(.*)/
=> /([0-9]*)([^\+ -]+)(.*)/

The only thing that it different in the 2 regexps is the placement of
the space in the square brackets, yet the first regexp is invalid and
the 2nd valid.

Why is this?

Thanks,
Dan
It's the placement of the - that makes a difference. In a character
class, - between two characters denotes a range. So the first
character class includes a range, from + to <space>, which is invalid
because + comes after space in the relevant character encoding.

If you want a literal hyphen in a character class, it's safest to make
it the last character.

-A
 
C

Christopher Schneider

I'm pretty sure the dash needs to be escaped in regular expressions.
The second one works since it is the last character in the character
class, and hence isn't defining a range.

/([0-9]*)([^\+\- ]+)(.*)/ should work - note the escaped dash.


-Chris Schneider
 
W

Wilson Bilkovich

irb(main):002:0> regex = /([0-9]*)([^\+- ]+)(.*)/
SyntaxError: compile error
(irb):2: invalid regular expression: /([0-9]*)([^\+- ]+)(.*)/
from (irb):2

irb(main):003:0> regex = /([0-9]*)([^\+ -]+)(.*)/
=> /([0-9]*)([^\+ -]+)(.*)/

The only thing that it different in the 2 regexps is the placement of
the space in the square brackets, yet the first regexp is invalid and
the 2nd valid.

The hyphen in the middle expression is ambiguous, because it could
either be a range or a literal.
One way is to rearrange the order so that it comes first:
/([0-9]*)([^-+ ]+)(.*)/
 
J

Junnichi Ohno

Daniel said:
irb(main):002:0> regex = /([0-9]*)([^\+- ]+)(.*)/
SyntaxError: compile error
(irb):2: invalid regular expression: /([0-9]*)([^\+- ]+)(.*)/
from (irb):2

irb(main):003:0> regex = /([0-9]*)([^\+ -]+)(.*)/
=> /([0-9]*)([^\+ -]+)(.*)/

The only thing that it different in the 2 regexps is the placement of
the space in the square brackets, yet the first regexp is invalid and
the 2nd valid.

Why is this?

Thanks,
Dan

Hi,

'-' shuld be escaped like this.
regex = /([0-9]*)([^\+\- ]+)(.*)/

Jun
 
D

David Kastrup

Christopher Schneider said:
I'm pretty sure the dash needs to be escaped in regular expressions.
The second one works since it is the last character in the character
class, and hence isn't defining a range.

/([0-9]*)([^\+\- ]+)(.*)/ should work - note the escaped dash.

Rubbish. In character ranges, \ is not special.
 
K

Ken Bloom

Christopher Schneider said:
I'm pretty sure the dash needs to be escaped in regular expressions.
The second one works since it is the last character in the character
class, and hence isn't defining a range.

/([0-9]*)([^\+\- ]+)(.*)/ should work - note the escaped dash.

Rubbish. In character ranges, \ is not special.

It most certainly is special:
irb(main):014:0> /[a\+\- ]/=~".\\"
=> nil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top