1

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.

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Works for your nice solution !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.