vendredi 17 avril 2015

Find a specific pattern (regular expression) in a list of strings (Python)

So I have this list of strings:



teststr = ['1 FirstString', '2x Sec String', '3rd String', 'x forString', '5X fifth']


What I need to do, is to get all the elements from this list that contain:



number + "x"


or



number + "X"


For example if I have the function



def SomeFunc(string):
#do something


I would like to get an output like this:



2x Sec String
5X fifth


I found somewhere here in StackOverflow this function:



def CheckIfContainsNumber(inputString):
return any(char.isdigit() for char in inputString)


But this returns each string that has a number.


How can I expand the functions to get the desired output?


Aucun commentaire:

Enregistrer un commentaire