开发者

Match number between characters

开发者 https://www.devze.com 2023-01-26 14:56 出处:网络
Here is some examples 158811_23.jpg151_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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号