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

How does adding a (500x5000) and (5000x1) matrix result in a (500x5000) matrix?
Merging arrays of varying size in Python
How would you Export a file with data in a table form in python?
CDF in Python not displaying correctly
Using cPickle for multiple regression statsmodel formula in Python Memory Error
Looping Through List Returns Negative Index
What's the pythonic way to chain if statements given n variables?
Python-Opencv error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
SOAPpy on a client behind proxy?
Python script runs in bash, but not in cron?
Pandas dataframe rounding values in columns
Unmet dependencies stops Rednotebook from installing on Ubuntu 16.04
Simple adding in Python
Need of abs () method while plotting a power spectral density for a given dataset
Join two RDDs on custom function - SPARK
Efficiently replace numbers in list of strings with a token

Categories

HOME
tensorflow
jdbc
crystal-reports
pug
itext
angular2-routing
jbehave
avro
chaiscript
recyclerview
webdav
laravel-4
iis-7.5
vsftpd
i2c
eval
octopus-deploy
eclipse-cdt
symfony-forms
symfony2-easyadmin
multiple-monitors
lenskit
aspell
uicollectionview
movies
statusbar
delete-file
hanami
metis
lync-2013
kong
skygear
salesforce-chatter
node-horseman
gettext
powershell-remoting
broadleaf-commerce
swagger-editor
declare
magiczoomplus
tic-tac-toe
flex4.5
dojox.mobile
ivona
gd
ti-basic
mcrypt
mercury
gmt
ingres
rsqlite
keycode
pdflib
xib
git-annex
multiple-file-upload
jackson-databind
gradle-eclipse
achievements
emokit
mpeg-4
multinomial
cctray
insertion-sort
schtasks.exe
enyo
selendroid
mogrify
lines-of-code
scrollspy
pisa
author
io.js
coin-flipping
wss
db4o
visual-studio-addins
arangodb-php
magicalrecord-2.2
windows-mobile-6
stagefright
google-closure-library
pep8
zend-framework-modules
osi
deploying
applicationcontext
law-of-demeter
associative
chatroom
sitemappath

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