简体   繁体   中英

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

I am pretty new to regex and a little confused with my example :

I have titles like theses :

  • Superman
  • Superman HD
  • Superman (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.

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 " ".

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 .

If it's everything after the superman then use this positive lookbehind:

(?<=Superman).*

If it's just HD or (HD) with or without , then:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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