Home > Software design >  Why I can't get correct answer in this regex?
Why I can't get correct answer in this regex?

Time:08-13

Why I can't get correct answer in this regex?

I wanted to get Microsoft Edge in Unknown - abc - Microsoft Edge

But I failed, I got this :

Question Picture:

Here is my code:

import re

content = 'Unknown - abc - Microsoft Edge'
p = re.compile(r"- .*?")
print(p.findall(content))

As the picture said:

It only showed: ['- ', '- '], but not Microsoft Edge

Please help me.

Thank you very much.

CodePudding user response:

You could use a rplit in this particular case (content.rsplit('- ', 1)[-1]), however if you insist on a regex, you can use:

p = re.compile(r"(?<=- )[^-]*$")  # non "-" after "- "
print(p.findall(content))

or

p = re.compile(r"(?<=- )(?:(?!- ).)*$")  # anything after "- " but not containing "- "
print(p.findall(content))

output: ['Microsoft Edge']

CodePudding user response:

Without knowing what other input values are possible and what other contents should/shouldn't be matched, it is hard to design this. p = re.compile(r"[^ ]* [^ ]*$") could work.

  • Related