Daniel Chepenko
Daniel Chepenko

Reputation: 2268

Split string using regular expression in python

I have such string

Sale: \t\t\t5 Jan \u2013 10 Jan

I want to extract the start and the end of the sale. Very straightforward approach would be to make several spilts, but I want to that using regular expressions. As the result I want to get

start = "5 Jan"
end = "10 Jan"

Is it possible to do that using regex?

Upvotes: 0

Views: 2928

Answers (2)

Austin
Austin

Reputation: 26039

This may not be an optimised one but works assuming the string pattern remains the same.

import re
s = 'Sale: \t\t\t5 Jan \u2013 10 Jan'
start, end = re.search(r'Sale:(.*)', s).group(1).strip().replace('\u2013', ',').split(', ')

# start <- 5 Jan
# end <- 10 Jan

Upvotes: 1

Rakesh
Rakesh

Reputation: 82765

This should help.

import re
s = "Sale: \t\t\t5 Jan \u2013 10 Jan"
f = re.findall(r"\d+ \w{3}", s)
print f

Output:

['5 Jan', '10 Jan']

Upvotes: 3

Related Questions