Here is some examples 158811_23.jpg 151_188.gif
开发者_StackOverflow中文版How to match the number between _
and .jpg
?
If you want to match the numbers between _
and .jpg
but exclude the _
and .jpg
then use:
(?<=\_)[0-9]+(?=\.jpg)
(?<=\\_)
is called a lookbehind. Anything after the _
except the _
(?=\\.jpg)
is called a lookahead. Anything before the .jpg
excluding the .jpg
Use this to segregate your numbers into the group $1 gif etc. are in $2
_(\d+)\.(gif|jpg)
This fairly simple regular expression should match the number you need as the first group:
[0-9]*_([0-9])*\.(?:jpg|gif)
It works for both .jpg and .gif files, since you used both in your examples.
精彩评论