Regex matching not enough digits -


when use regex pattern:

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

on these strings, here :

[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 catch ending numbers (1, 2, 3, 14, 15, 16, 127) keeping easy .* @ beginning (for simplicity) ?

you should use word boundaries , anchor $:

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

it because .* greedy , without \b matches more needed.

.* not needed if must use then:

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

regex demo


update: based on edited question there no word boundary before numbers, can use:

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

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

regex demo2


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -