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

Is it safe to use scipy.sparse functions with Pandas sparse dataframes?
I got error : UnboundLocalError: local variable 'porc' referenced before assignment
Trigger a delete when inserting a new entry on django
How does gauss laguerre integration works for large limits?
Selenium: Runtime.executionContextCreated has invalid 'context':
Python 3.5 csv input shows extra character 'b'
Python Error 104, connection reset by peer
css not getting loaded in flask webpage?
Regarding the regex in search module with and without raw text
How to calculate the mean of a column by decade in Python
How to read webpages that are without .htm* extension using Python?
can I use python's 'socket' module to connect to a wireless ethernet host?
Requests VS Urllib 2 [closed]
Call python class from another Python script
How can I use BeautifulSoup to get a few contents that comes after a specific text on a page?
Reinstall python 2.7.12 and python 3.5.2

Categories

HOME
cluster-computing
cil
project-management
reverse-engineering
freepascal
mapping
paramiko
puppet
tomcat7
csvhelper
chaiscript
terrain
phonegap-cli
hyperledger-fabric
snap.svg
wordpress-theming
sign
react-leaflet
sendkeys
reduction
alfresco-share
cairo
session-timeout
grub2
ng-show
eclipselink
styles
cython
spring-ws
shopping-cart
spring-data-neo4j
vcard
i3
system.data.sqlite
hana-studio
google-api-dotnet-client
backup-strategies
body-parser
ocs
viewstate
ksoap
discord.net
productivity
rdw
mouseclick-event
jgraph
xmgrace
cdk
jszip
jcreator
unity3d-editor
garrys-mod
build-process
payu
alter
tomee
galleriffic
pydio
webkit2
soda
nssplitview
contour
emokit
gemini
codesign
django-south
flexjson
android-viewholder
enyo
response-headers
sevenzipsharp
pyrocms
achartengine
twitter-rest-api
coypu
django-1.6
inputaccessoryview
activity-streams
line-numbers
libsndfile
kendo-mvvm
livechat
iostream
dto
csplit
adaptive-compression
spdy
indexing-service
dynamic-proxy
robospice
onselect
vertical-rhythm
xslkey
icefaces-3
redirectstandardoutput
legacy-code
ninject-extensions
windows-phone-7-emulator
isolatedstorage
file-exists
jpf
supersized
flexicious
fileutils
phpcrawl
idictionary
configurable
visual-studio-dbpro

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