Home > Mobile >  How to search for A (any lengths any character) * in Python 3
How to search for A (any lengths any character) * in Python 3

Time:09-28

I have a string with letters and occasionally *. I am trying to write regular expressions in Python 3 to get anything in the string that starts with A and ends with *, things in between can be any letter but not *. Help is much appreciated.

CodePudding user response:

Try using the regex pattern \bA\w*\*(?!\S):

inp = "An example is an A* and also an Abc*"
matches = re.findall(r'\bA\w*\*(?!\S)', inp)
print(matches)  # ['A*', 'Abc*']

CodePudding user response:

There is a simpler pattern one can use with python regex.

r"(A. ?\*)"

For more on "Greedy vs Non-Greedy in Python": https://docs.python.org/3/howto/regex.html#greedy-versus-non-greedy

An example:

import re
string_to_test = "This is Atest1* but not this* but Atest2* done *a asdf"
for x in re.findall(r"(A. ?\*)", string_to_test):
    print(x)

This will produce:

Atest1*
Atest2*

Note that depending on if the string "A*" is valid or not you might need the pattern r"(A.*?\*)"

  • Related