1

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)?

  • Next time, try to be a bit more precise in specifying your environement like language, OS and so on. – Keltia Jan 15 '09 at 09:35

4 Answers4

3

Something like

/^\d(\d\d)/

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

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • +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 Jan 15 '09 at 09:30
  • Showing your delimter also helps to indicate what chars you may or may not have escaped – Paul Dixon Jan 15 '09 at 10:07
  • just trying to protect the OP if he was on a MS/.net language.. no righteous indignation intended :) – Gishu Jan 15 '09 at 10:21
1
^\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
  • 14,535
  • 3
  • 29
  • 30
1

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
  • 2,619
  • 5
  • 31
  • 45
0

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}$'
leo
  • 3,677
  • 7
  • 34
  • 46