Reputation: 43
I have a string:
The estimated delivery time will be approximately 5 - 7 business days from the time of order.
I want to extract: 5-7 business days
from this string.
I wrote regex: '(^[[0-9][-]]*.*$)'
But it does not works. Thanks.
Upvotes: 0
Views: 164
Reputation: 2819
import re
s="The estimated delivery time will be approximately 5 - 7 business days from the time of order."
re.search('\d+\s*\-\s*\d+.*days',s).group(0)
Upvotes: 0
Reputation: 7297
You can use re.search('approximately([\s\S]+business\s+days)',s).group(1)
. Grouping used to get desired result in regex match/search.
Upvotes: 0
Reputation: 71598
Your regex is a bit strange...
Try:
r'([0-9]+\s*-\s*[0-9]+) business days'
^
and $
are anchors and will match the beginning and end of the string, which I don't think is what you want here. Also, capture groups is not really necessary, so r'[0-9]+\s*-\s*[0-9]+ business days'
should work just fine.
I added the quantifiers +
just in case there are more business days. and \s*
to provide for any possible spaces.
In your regex, you were having two character classes [[0-9]
and [-]
and a single literal ]
there.
The first character class will match any of [
, or any number. The second will match a single hyphen.
The r
at the front just makes the string become a raw string. It's usually safer to use raw strings in regexes.
Upvotes: 2