简体   繁体   English

Python Regex:在匹配特定的字符串和空格或逗号之前

[英]Python Regex : Match a particular string and space or coma before

I am pretty new to regex and a little confused with my example : 我对regex相当陌生,对我的示例有些困惑:

I have titles like theses : 我有这样的头衔:

  • Superman 超人
  • Superman HD 超人高清
  • Superman (HD) 超人(HD)
  • Superman,HD 高清超人
  • Superman,(HD) 超人(高清)
  • Hands on 动手
  • Hands, on 双手

What I want to do is match whatever HD or (HD) that will be at the end of the title and also the "," or " " that is just before. 我想做的是匹配标题结尾处的任何HD或(HD),以及前面的“,”或“”。

Typically here with my example, I want to match everything but the "Superman" and match nothing in the last 2 titles. 通常在这里以我的示例为例,我想匹配除“超人”以外的所有内容,并且不匹配最后两个标题中的任何内容。

I am trying something like that : 我正在尝试类似的东西:

[\s,HD|\(HD\))]

But it's also matching all the other "H" "D" "," and " ". 但它也匹配所有其他“ H”,“ D”,“”和“”。

Can anyone give a small help ? 有人可以帮忙吗?

Use this pattern: 使用以下模式:

/(?:,| )(?:HD|\(HD\))/

Online Demo 在线演示

您需要在正则表达式中使用行尾标记$ ,并使其他字符为可选:

/\s*,?\(?HD\)?$/

For your example, please try this pattern: 对于您的示例,请尝试以下模式:

/(?<=\bSuperman)(\s*,?(?:HD|\(HD\)))/g

EXPLANATION: 说明:

(?<=\bSuperman)  # positive lookbehind to assert 'Superman' is behind
\s*              # match 0 or more spaces
,?               # match literal , 0 or one time
(?:HD|\(HD\))    # the ending could be 'HD' or '(HD)'

REGEX 101 DEMO . REGEX 101演示

If it's everything after the superman then use this positive lookbehind: 如果这是超人之后的一切,那么请使用以下积极的表情:

(?<=Superman).*

If it's just HD or (HD) with or without , then: 如果只是具有或不具有的HD或(HD),则:

(?<=Superman)\s?,?HD|\(HD\)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM