deanrihpee
deanrihpee

Reputation: 178

How to detect number between underscore ("_")?

Sorry first, im not good at Regex but anyone can help me ? i have file that named :

Filename - 01.exe

Filename - 001.exe

Filename - 0001.exe

Filename_01_.exe

I have tried with this Regex

(?=.*?)\d{1,4}\b

But that just detect 01, 001, 0001 number at Filename - 01.exe, Filename - 001.exe and Filename - 0001.exe, not worked with Filename_01_.exe Is my Regex is wrong or there Alternative Regex or Method ?

Sorry if my english is not well.

Upvotes: 3

Views: 779

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

If you want only 01 from this Trailer ep 01 720p.mp4 example, then use word boundaries,

\b[0-9]+\b

\b matches between a word character and a non-word character.

And the below regex would match the number which are located in between word boundaries or _ symbol.

(?:_|\b)\K[0-9]+(?=_|\b)

DEMO

Upvotes: 1

zx81
zx81

Reputation: 41838

Since there is only one number in the file, how about simply using this:

\d+

In C#:

var myRegex = new Regex(@"\d+");
string resultString = myRegex.Match(yourString).Value;
Console.WriteLine(resultString);

Upvotes: 2

Related Questions