python


Add optional part in python regular expression


I want to add an optional part to my python expression:
myExp = re.compile("(.*)_(\d+)\.(\w+)")
so that
if my string is abc_34.txt, result.group(2) is 34
if my string is abc_2034.txt, results.group(2) is still 34
I tried myExp = re.compile("(.*)_[20](\d+)\.(\w+)")
but my results.groups(2) is 034 for the case of abc_2034.txt
Thanks F.J.
But I want to expand your solution and add a suffix.
so that if I put abc_203422.txt, results.group(2) is still 34
I tried "(.*)_(?:20)?(\d+)(?:22)?.(\w+)")
but I get 3422 instead of 34
myExp = re.compile("(.*)_(?:20)?(\d+)\.(\w+)")
The ?: at the beginning of the group containing 20 makes this a non-capturing group, the ? after that group makes it optional. So (?:20)? means "optionally match 20".
strings = [
"abc_34.txt",
"abc_2034.txt",
]
for string in strings:
first_part, ext = string.split(".")
prefix, number = first_part.split("_")
print prefix, number[-2:], ext
--output:--
abc 34 txt
abc 34 txt
import re
strings = [
"abc_34.txt",
"abc_2034.txt",
]
pattern = r"""
([^_]*) #Match not an underscore, 0 or more times, captured in group 1
_ #followed by an underscore
\d* #followed by a digit, 0 or more times, greedy
(\d{2}) #followed by a digit, twice, captured in group 2
[.] #followed by a period
(.*) #followed by any character, 0 or more times, captured in group 3
"""
regex = re.compile(pattern, flags=re.X) #ignore whitespace and comments in regex
for string in strings:
md = re.match(regex, string)
if md:
print md.group(1), md.group(2), md.group(3)
--output:--
abc 34 txt
abc 34 txt
Not sure if you're looking for this, but ? is the re symbol for 0 or 1 times. or {0,2} which is a bit hacky for up to two optional [0-9]. I will think more on it.

Related Links

Delete some elements from numpy array
ElasticSearch throwing mapper parsing exception when indexing JSON array of integers and strings
Using one PhantomJS browser for each worker rather than creating new instance for each URL in queue slows performance dramatically
Extract OneToOne Field in django model
In python solve for a matrix with restrictions
Setting up the EB CLI - error nonetype get_frozen_credentials
Saving a csv with excel so python can append rows correctly
PyCharm module importing stopped working properly (OS X)
string in index out of range grok learning
How to scrape a url having no of pages
Multiple foreign key fields in abstract Django class
How to convert the value_list to list in Django
Implicit Differentiation Sympy
Why does pygame stop working when I remove a print statement?
Interact with an interactive shell script on python
Need help connecting to SQL Server from Python Flask-Appbuilder

Categories

HOME
sql-server-2008
makefile
json
swift
google-contacts
memory-leaks
command
terrain
ll
snap.svg
desktop
nixos
postmessage
riak
dendrogram
alfresco-share
apache-karaf
zoomcharts
arraylist
ng-show
google-api-java-client
zoho
url.action
delete-file
bcrypt
philips-hue
opencart2.3
weinre
superagent
invoke-command
ivy
jboss5.x
protobuf-net
graph-databases
node-horseman
css-counter
jquery-cycle2
ksoap
socketpair
elfinder
concrete5-5.7
magiczoomplus
haskell-pipes
soundjs
nashorn
lftp
gist
ogre3d
crystal-reports-8.5
collapsingtoolbarlayout
svn-merge
uistackview
mailcatcher
angularjs-ng-pattern
dts
spoofing
image-editing
persistence.xml
android-viewholder
evo
bridge
cpu-cores
callstack
jazz
virus
word-2013
hana-xs
belongs-to
cocoascript
grunt-contrib-concat
suffix-array
facebook-wall
magic-numbers
jstack
hamsterdb
magicalrecord-2.2
real-time-updates
osx-snow-leopard
fogbugz-api
sql-server-2012-web
backbone-relational
grunt-contrib-compass
fieldset
attachevent
cosm
coff
posting
rubycas
for-xml-path
carbide
hadoop-plugins
camtasia
webrat
scatterview
usergroups
divx

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