当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Regular expression to match video file names

问题描述:

To be more specific I do not know what the exact values will be.

Essentially i need to find 3 or 4 digit characters followed by the literal letter p?

ie 1080p, 720p, 576p etc...

Looked around a lot of stuff but just couldnt make it work.

Heres' an example string

Back.to.the.Future.1985.720p.BluRay.X264-AMIABLE.mkv

In this case I want to return

Back.to.the.Future.1985.


re.search("^[^\d{3,4}p]+","Back.to.the.Future.1985.720p.BluRay.X264-AMIABLE.mkv")

however, returns

'Back.to.the.Future.'

Thanks a lot

网友答案:
>>> import re
>>> re.search("(.*)\d{3,4}p",
              "Back.to.the.Future.1985.720p.BluRay.X264-AMIABLE.mkv").groups()[0]
'Back.to.the.Future.1985.'
网友答案:

i need to find 3 or 4 digit characters followed by the literal letter p

\d{3,4}p

If you do not want to match 3456p in 123456poop you can add some assertions, like:

\b\d{3,4}p\b
网友答案:

It sounds like what you want is "anything, followed by one or more digits, followed by 'p'". Then you want to 'capture' the "anything" part.

That looks like

(.*)\d+p

Or to specify the range of digit quantities,

(.*)\d{3,4}p
分享给朋友:
您可能感兴趣的文章:
随机阅读: