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

Using numpy to square value gives negative number
Execute cURLs command line in Python
Python subprocess.Popen and stdin: No such file or directory
Python: Include complete package in another package
How to optimize a black box function in Python?
Track download progress of S3 file using boto3 and callbacks
Remove elements of one list from another list using “while”
Hosting Bokeh Server with Tornado on AWS
Functions that help to understand json(dict) structure
Scapy How converting hex to new packet
Write and read test files in dictionary format
Python 3.x Connect to account with request POST
AttributeError: module 'networkx' has no attribute 'Graph'
How to exclude selected domains from python crawl script in google search
Python - Py2exe error
Anaconda import packages from another environment

Categories

HOME
visual-studio-2015
admin-on-rest
openssl
jar
antivirus
appmaker
algorithmic-trading
yocto
iis-7.5
triggers
google-plus
uiactivityviewcontroller
game-maker-studio-1.4
public-key-encryption
mongodb-query
typeahead
opera-mini
phpbb
aptana
piwik
rworldmap
scalajs-react
python-textprocessing
visual-c++-2017
helper
dpi
multichoiceitems
configure
pitest
read-write
google-drive-realtime-api
pox
pace
autocompletetextview
dql
fltk
delphi-10.1-berlin
cgo
jupyter-console
nashorn
distributed-transactions
w3-total-cache
ideone
greenhills
nsfetchedresultscontrolle
push-diffusion
django-filer
gradle-eclipse
mongo-c-driver
date-format
axes
anti-patterns
pyrocms
playscape
ffserver
xaml-designer
epl
stackframe
chaining
errorprovider
funq
getimagedata
surrogate-key
bullet
lumx
flash-cc
mimosa
jqmobi
ftp4j
lame
objcmongodb
htmltextwriter
arbor.js
transitive-closure-table
isolatedstorage
entity-framework-ctp5
handwriting
j-interop
hadoop-plugins
yajl

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