Carlos Agustin
Carlos Agustin

Reputation:

Regex match and grouping

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

Upvotes: 1

Views: 1224

Answers (4)

leo
leo

Reputation: 3749

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}$'

Upvotes: 0

Dana
Dana

Reputation: 2739

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.

Upvotes: 1

Keltia
Keltia

Reputation: 14743

^\d(\d\d)

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

Upvotes: 1

Paul Dixon
Paul Dixon

Reputation: 300825

Something like

/^\d(\d\d)/

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

Upvotes: 3

Related Questions