-1

My input is--

Text = "My name is Alex , house no - 254456845 with mobile no. +91-11-22558845 and my account no. is - AB12569BF214558."

Now I want to use regex (re.findall) on my input text and get the expected output.

Expected Output= ( it contains both digits and alphabet and is always of length = 15 )

[ "AB12569BF214558" ]

please help

2
  • Text[-15:] maybe? Commented Jan 5, 2021 at 5:32
  • The duplicate link given is not, IMHO, enough because it doesn't include the exact regex logic/approach which is needed here. Commented Jan 5, 2021 at 6:40

2 Answers 2

0

You should be using the following regex pattern:

\b[A-Z0-9]{15}\b

This will ensure that you match a string of uppercase/numbers which is exactly 15 characters in length. Sample script:

Text = "My name is Alex , house no - 254456845 with mobile no. +91-11-22558845 and my account no. is - AB12569BF214558."
matches = re.findall(r'\b[A-Z0-9]{15}\b', Text)
print(matches)

This prints:

['AB12569BF214558']
Sign up to request clarification or add additional context in comments.

Comments

0
import re
results = re.findall('\\b[A-Z0-9]{15}\\b','''My name is Alex , house no - 254456845 with mobile no. +91-11-22558845 and my account no. is - AB12569BF214558.''')

4 Comments

this solution gives me my expected output in this case, but when i use this code in other cases then i gives me all the strings that have length 15 or more, i just want that string length should be strictly 15 only and not other string with length more than 15 should be given in output.
The {15} means exactly 15 characters. Fifteen or more would be {15,}
Oh I think I understand what you mean. The result is only 15 characters, but it is part of a longer string. Adding the '\b' at the start and end per Tim B's answer takes care of that.
@MukulKant Please don't mark code only answers like this. Otherwise you will get banned from reviewing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.