Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: String formatting a regex string that uses both '%' and '{' as characters

Tags:

python

regex

I have the following regular expression, which lets me parse percentages like '20%+', '20%', or '20% - 50%' using re.split.

'([0-9]{1,3}[%])([+-]?)'

I want to use string formatting to pass the series identifiers (i.e. '+-') as an argument from config.py.

SERIES = '+-'

The two methods I've tried produced errors. New-style formatting runs into the following error (due to the {m,n} usage):

>>> import config
>>> regex = '([0-9]{1,3}[%])([{0}]?)'.format(config.SERIES)
KeyError: '1,3'

Old-style formatting has its own problems (due to the '%' character):

>>> import config
>>> regex = '([0-9]{1,3}[%])([%s]?)' % (config.SERIES)
unsupported format character ']' (0x5d) at index 14

I haven't been able to get escape characters working inside the regex. Any ideas on how do do this?

Thanks,

Mike

like image 342
MikeRand Avatar asked Nov 16 '10 22:11

MikeRand


People also ask

What is %s and %D Python?

%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values. For example (for python 3) print ('%s is %d years old' % ('Joe', 42)) Would output Joe is 42 years old.

What are the two types of characters used in regular expression in Python?

Regular expressions use two types of characters in the matching pattern string: Meta characters are characters having a special meaning, similar to * in wild card. Literals are alphanumeric characters. Following list of characters are called the metacharacters.


1 Answers

You can use %% to insert a percent-sign using the old-style formatting:

'([0-9]{1,3}[%%])([%s]?)' % (config.SERIES)

Similarly for the new-style formatting, double the braces:

'([0-9]{{1,3}}[%])([{0}]?)'.format(config.SERIES)
like image 84
Ray Avatar answered Sep 28 '22 19:09

Ray