Basj
Basj

Reputation: 46483

Regex matching not enough digits

When I use this regex pattern:

.*(?P<midinote>\d+)\.wav

on these strings, here is what I get :

[ASR10] CHR Huge Ahhs1.wav     => midinote=1 OK
[ASR10] CHR Huge Ahhs2.wav     => midinote=2 OK
[ASR10] CHR Huge Ahhs3.wav     => midinote=3 OK
[ASR10] CHR Huge Ahhs14.wav    => midinote=4 NOT OK
[ASR10] CHR Huge Ahhs15.wav    => midinote=5 NOT OK
[ASR10] CHR Huge Ahhs16.wav    => midinote=6 NOT OK
[ASR10] CHR Huge Ahhs127.wav   => midinote=7 NOT OK

How to catch the ending numbers (1, 2, 3, 14, 15, 16, 127) by keeping an easy .* at the beginning (for simplicity) ?

Upvotes: 2

Views: 50

Answers (2)

anubhava
anubhava

Reputation: 785156

You should use word boundaries and anchor $:

\b(?P<midinote>\d+)\.wav$

It is because .* is greedy and without \b it matches more than needed.

.* is not needed but if you really must use it then:

.*\b(?P<midinote>\d+)\.wav$

RegEx Demo


Update: Based on edited question where there is no word boundary before numbers, you can use:

.*?(?P<midinote>\d+)\.wav$

i.e. make .*? non-greedy.

RegEx Demo2

Upvotes: 2

Brian Stephens
Brian Stephens

Reputation: 5261

You don't need the .* at all, since it has absolutely no effect on what matches. And, as @anubhava stated, it's greedy, so it's consuming some of the digits you want to match.

Just simplify it to this:

(?P<midinote>\d+)\.wav

Upvotes: 0

Related Questions