简体   繁体   中英

Regex to find specific number using Python regex

I need a regex to find the maxtimeout value (40 in the following) in the RequestReadTimeout directive in Apache config. file, for example :

RequestReadTimeout header=XXX-40,MinRate=XXX body=XXX
RequestReadTimeout header=40 body=XXX

PS : XXX refer to a decimal digit

I used this :

str="RequestReadTimeout header=10-40,MinRate=10 body=10"
re.search(r'header=\d+[-\d+]*', str).group()
'header=10-40'

But I need a regex to get only the maxtimeout value (40 in this example) in one row (without using other function like spit("-")[1] ...etc).

Thanks.

You'd group the part you wanted to extract:

re.search(r'header=(?:\d*-)?(\d+)', inputstr).group(1)

The (...) marks a group, and positional groups like that are numbered starting at 1.

I altered your expression a little to only capture the number after an optional non-capturing group containing digits and a dash, to match both patterns you are looking for. The (?:...) is a non-capturing group; it doesn't store the matched text in a group, but does let you use the ? quantifier on the group to mark it optional.

Pythex demo .

Python session:

>>> import re
>>> for inputstr in ('RequestReadTimeout header=1234-40,MinRate=XXX body=XXX', 'RequestReadTimeout header=40 body=XXX'):
...     print re.search(r'header=(?:\d*-)?(\d+)', inputstr).group(1)
... 
40
40

You could do it with the following regex :

'RequestReadTimeout\\sheader=(?:\\d+)?-?(\\d+).*'

The first captured group \\1 is what you want

Demo: http://regex101.com/r/cD6hY0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM