python


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)

Related Links

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)

Categories

HOME
sql-server-2008
drupal-7
textwatcher
ocaml
google-contacts
puppet
memory-leaks
google-sheets-api
extract
phonegap-cli
tizen-wearable-sdk
keystore
cartodb
ndis
cross-platform
aspell
mule-studio
rhapsody
zoomcharts
google-openid
anychart
ng-show
jogl
jni
jquery-form-validator
tarantool
novnc
scalajs-react
samsung-mobile
onesignal
ui5
owl-api
mayavi
superscript
getjson
gettext
windows-10-iot-core
google-guava-cache
jboss-esb
wfp
socketpair
twiki
web-development-server
nsexception
productivity
stereo-3d
tango
php-ews
automator
vcf
tuleap
defold
greenhills
selenium-firefoxdriver
autoresize
cleditor
cfeclipse
hspec
uistackview
spoofing
nofollow
multilingual-app-toolkit
freefem++
inet
multinomial
lemoon
java.util.calendar
getrusage
data-import
lmfit
rad
registrykey
windowlistener
qbfc
mogrify
apache-mina
jai
hana-xs
jcr-sql2
left-recursion
mov
genetic-programming
alpha-transparency
io.js
data-generation
kraken.js
bigint
robospice
installshield-2009
stagefright
shortcuts
qss
htmltextwriter
returnurl
macruby
parameterization
visual-web-gui
emacs23
serp
testunit
junit3
.app
open-graph-beta
hardware-acceleration
lazy-c++
nt4
virtualquery
castle-validators
outlook-form
web-analytics-tools
document-library

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App