0

I have a problem to find a string/float inside a string after serveral metacharacters with Python. I watched videos and read a lot of explanations, but for my special case I didn't find a solution, maybe you can help. I have a string with backslashs and asterisk and I want JUST the float after the string "Score" - how can I get it?

text = str("texttexttext\n*Score* 2,567 texttexttext")
y = re.findall("\\n*Score*(.+)", text)
print(y)

Output: ['* 2,567 texttexttext']
# I want JUST the Output "2,567"

Other Question: How can I get JUST the text before "\nScore"?

z = re.findall("^(.+)\\n*Score*", text)
print(z)
#no Output

Thanks for helping me!

Chrissi
  • 95
  • 1
  • 1
  • 10
  • 1
    Why `text = str("texttexttext\n*Score* 2,567 texttexttext")` and not `text = "texttexttext\n*Score* 2,567 texttexttext"`? Anyway, the solution is known and simple: `re.findall(r"Score\D*(\d+(?:,\d+)?)", text)` or `re.findall(r"Score\D*(\d[\d,]*)", text)` – Wiktor Stribiżew Dec 15 '20 at 11:10
  • And for JUST the text before "\nScore"? I'am sorry for being not such a good programmer to find this simple solution. That's why I am asking here. – Chrissi Dec 15 '20 at 11:30
  • 1
    Why regex at all? Use `text[:text.index("\\n*Score*")]`, see [this thread](https://stackoverflow.com/questions/27387415/how-would-i-get-everything-before-a-in-a-string-python/27387468). – Wiktor Stribiżew Dec 15 '20 at 11:32
  • so this question is showed as duplicate, should I delete it now? I got a bad rating and can't ask questions anymore. – Chrissi Jan 06 '21 at 11:54
  • 1
    I suggest you keep the question, it is not bad. – Wiktor Stribiżew Jan 06 '21 at 11:57
  • wow I don't know what happened, I did just some small changes in my question above, but now I can ask questions again! thank you! – Chrissi Jan 06 '21 at 12:53

0 Answers0