tags:

views:

87

answers:

5

Here's a sample string which I want do a regex on

101-nocola_conte_-_fuoco_fatuo_(koop_remix)

The first digit in "101" is the disc number and the next 2 digits are the track numbers. How do I match the track numbers and ignore the disc number (first digit)?

+3  A: 

Something like

/^\d(\d\d)/

Would match one digit at the start of the string, then capture the following two digits

Paul Dixon
+1 to ^\d(\d\d) : the ending and trailing / are not part of the regex.. but rather a regex delimiter in ruby for example.
Gishu
Enclosing a regex within // is very common...
Keltia
Showing your delimter also helps to indicate what chars you may or may not have escaped
Paul Dixon
just trying to protect the OP if he was on a MS/.net language.. no righteous indignation intended :)
Gishu
heh, none taken :)
Paul Dixon
+1  A: 
^\d(\d\d)

You may need \ in front of the ( depending on which environment you intend to run the regex into (like vi(1)).

Keltia
+1  A: 

Do you mean that you don't mind what the disk number is, but you want to match, say, track number 01 ?

In perl you would match it like so: "^[0-9]01.*"
or more simply "^.01.*" - which means that you don't even mind if the first char is not a digit.

Dana
You forgot the grouping...
Keltia
A: 

Wow, thanks guys!

A: 

Which programming language? For the shell something with egrep will do the job:

echo '101-nocola_conte_-_fuoco_fatuo_(koop_remix)' | egrep -o '^[0-9]{3}' | egrep -o '[0-9]{2}$'