1

I wrote a regex that should recognise string: number. I do not get any errors but I get only string: without number

There a couple of variations of word that should represent string and number should be integer from 7 do 9 digits.

import re


text = 'Center Postfach 2250 Kundennummer: 90289963 CH-4002 Basel Frau Re'

regex = r"(kundennummer|kundennummer.|kundennummer:|numéro d'abonné||Kunden Nr.|No de client:)\s\d{7,9}"


gpnumber_list = re.findall(regex, text, re.IGNORECASE)
print(gpnumber_list) #['Kundennummer:']

Expected result:

Kundennummer: 90289963

1 Answer 1

2

You need to add another capturing group for the number.

regex = r"(kundennummer|kundennummer.|kundennummer:|numéro d'abonné||Kunden Nr.|No de client:)\s(\d{7,9})"

Notice parentheses around \d{7,9}.


To get a list of strings, as pointed out in the comment, you can simply join the matched groups

[" ".join(item) for item in gpnumber_list]
Sign up to request clarification or add additional context in comments.

2 Comments

It works but it returns tuple inside of the list, is there a way to return string in list?
You mean like "Kundennummer: 90289963"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.