Say, I have the following code:
import re
strings_of_text = ['data0', 'data23', 'data2', 'data55', 'data_mismatch', 'green']
strings_to_keep = []
expression_to_use = r'^\d{2}$|(?<=\s)\d{2}(?=\s)|(?<=\s)\d{2}$|^\d{2}(?=\s)'
for string in strings_of_text:
# If the string is data#
if (re.search(expression_to_use, string)):
strings_to_keep.append(string)
print(strings_to_keep)
Where I am only concerned with adding strings with the pattern "data" followed by some number. So in this case, I would only want to add 'data0', 'data23', 'data2', 'data55'
How can I do this? I am thinking I will need to import re but I'm not sure how to use it.
I have read this: Python Regular Expression looking for two digits only
But when I try to modify my regular expression using this expression
^\d{2}$|(?<=\s)\d{2}(?=\s)|(?<=\s)\d{2}$|^\d{2}(?=\s)
It does not work... This is where I am stuck. I am new to using regular expressions so thank you to all of those who post in advance
EDIT:
Here is the outcome I am trying to get:
print(strings_to_keep)
>>> ['data0', 'data23', 'data2', 'data55']