regex. Match all words starting with two letter
I wish to find all words that start with "Am" and this is what I tried so far with python import re my_string = "America's mom, American" re.findall(r'\b[Am][a-zA-Z]+\b', my_string) but this is the output that I get ['America', 'mom', 'American'] Instead of what I want ['America', 'American'] I know that in regex [Am] means match either A or m, but is it possible to match A and m as well?
The [Am], a positive character class, matches either A or m. To match a sequence of chars, you need to use them one after another. Remove the brackets: import re my_string = "America's mom, American" print(re.findall(r'\bAm[a-zA-Z]+\b', my_string)) # => ['America', 'American'] See the Python demo This pattern details: \b - a word boundary Am - a string of chars matched as a sequence Am [a-zA-Z]+ - 1 or more ASCII letters \b - a word boundary.
Don't use character class: import re my_string = "America's mom, American" re.findall(r'\bAm[a-zA-Z]+\b', my_string)
re.findall(r'(Am\w+)', my_text, re.I)
Python input validation and edge case handling
Struggling with making a Python module accessible via PyPi
Finding the minimum and maximum of a list of arrays
Accessing Google Drive Spreadsheets with Python Gspread
py.test & pytest on Raspberry Pi : Differences ?
Find maximum of column for each business quarter pandas
placeholder functions in sympy
Django: how to chain 2 add() calls in 1 create()?
How to specify large integer literals in a readable way?
How to do `PUT` on Amazon S3 using Python Requests
Untangle re.findall capturing groups: 'list' object has no attribute 'join'
Twisted equivalent of ThreadPoolExecutor (in Scrapy pipeline)
numpy.minimum applied to pandas DataFrame and Series
Find the area of a bounding polygon that encloses a set of points
Stacking Images in a Python graph
Deleting a directory if it doesn't contain a file of specific type (Python)