regexp problem

B

Brett S Hallett

I'm trying to detect the string "0,0,0" ( that's numeric comma numeric
comma numeric) which is a value returned by cdrecord -scanbus
for detecting CD-ROM drives attached to a Linux system.
I have tried numerous regexp expressions but cannot get that string to
be detected.

eg : "[0-9]\.[0-9]\,[0-9]"

I assume that the comma is causing the trouble but even using \ escape
( \,) does not solve the problem.

Help please, thanks
 
M

Michael campbell

Brett said:
I'm trying to detect the string "0,0,0" ( that's numeric comma numeric
comma numeric) which is a value returned by cdrecord -scanbus
for detecting CD-ROM drives attached to a Linux system.
I have tried numerous regexp expressions but cannot get that string to
be detected.

eg : "[0-9]\.[0-9]\,[0-9]"

I assume that the comma is causing the trouble but even using \ escape (
\,) does not solve the problem.

Help please, thanks

Your first "comma" there is a period, which would be ok except you've
escaped it to mean a literal "." instead of its meta-char meaning of
"any character".

/[0-9],[0-9],[0-9]/.match("0,1,2")

works for me.
 
R

Robert Klemme

Mike Stok said:
Robert Klemme said:
or for full integer match support /[+-]?\d+(,[+-]?\d+){2}/

Once I start repeating chunks of regular expressions I try to remember
to name them e.g.

Reasonable.
int = /[+-]?\d+/ => /[+-]?\d+/
/#{int}(?:,#{int}){2}/.match("0,1,2")
=> # said:
/#{int}(?:,#{int}){2}/.match("0,1,")
=> nil

This leads to interesting points:

Why not:

irb(main):010:0> int='[+-]?\d+'
=> "[+-]?\\d+"
irb(main):011:0> /#{int}(?:,#{int}){2}/o.match("0,1,2").to_a
=> ["0,1,2"]
irb(main):012:0> /#{int}(?:,#{int}){2}/o.match("0,1,")
=> nil
irb(main):013:0>

The difference being:

irb(main):021:0> int = /[+-]?\d+/
=> /[+-]?\d+/
irb(main):022:0> /#{int}(?:,#{int}){2}/
=> /(?-mix:[+-]?\d+)(?:,(?-mix:[+-]?\d+)){2}/
irb(main):023:0> int = '[+-]?\d+'
=> "[+-]?\\d+"
irb(main):024:0> /#{int}(?:,#{int}){2}/
=> /[+-]?\d+(?:,[+-]?\d+){2}/
irb(main):025:0>

i.e. why did you make 'int' a regular expression? And: one should use "o"
flag in case you break up the rx only for readability / typing effort.

Regards

robert
 

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

Forum statistics

Threads
474,138
Messages
2,570,803
Members
47,349
Latest member
jojonoy597

Latest Threads

Top