Matching URL of specific pattern
I'm trying match URLs like these:
something_with_underscore.png
something_with_underscore_mark.png
something_with_underscore_3600.png
something_with_underscore_10x10.png
something_with_underscore_10x10_mark.png
something_with_underscore_10x10_3600_mark.png
Using this regular expression:
(.+(?!(?:_[0-9]+x[0-9]+)?(?:_[0-9]+)?(?:_mark)?\.(?:jpg|png|gif)))((?:_([0-9]+)x([0-9]+))?(?:_([0-9]+))?(_mark)?)\.(jpg|png|gif)
This is basically two parts, part one capturing the name:
(.+(?!(?:_[0-9]+x[0-9]+)?开发者_C百科(?:_[0-9]+)?(?:_mark)?\.(?:jpg|png|gif)))
part two capturing "features":
((?:_([0-9]+)x([0-9]+))?(?:_([0-9]+))?(_mark)?)\.(jpg|png|gif)
I'm interested in capturing:
group 1: something_with_underscore
group 2: (everything after group 1)
group 3: (first integer 10 of 10x10 part)
group 4: (second integer 10 of 10x10 part)
group 5: (the integer 3600 or whatever it is - if is there)
group 6: _mark (if is there)
group 7: (png, jpg, gif)
I'm using negative lookahead with a pattern looking like the matching group to get the name of the file. I'm doing this to allow _ in the name-part of the url. This, however, fails. (if you can see another way of achieving this please do tell).
I think it has to do with the _ and . but I cannot seem to figure out how.
If for instance you remove the first _ from the expression the three last test cases will match correctly.
How about:
(.+?)(_?([0-9]+)?x?([0-9]+)?_?([0-9]+)?(_mark)?\.(jpg|png|gif))
Is this what you are looking for? It matches all your examples.
([^\.]+)(([0-9]+x[0-9]+)?)((_3600)?)((_mark)?)(\.(jpg|png|gif))
精彩评论