Tom
Tom

Reputation: 9653

python - How to know what re used to split a string?

The code below lets a user input two names of movies separated by either &, | or ^:

query = raw_input("Enter your query:")
movie_f = re.split('&|\^|\|', query)[0].strip()
movie_s = re.split('&|\^|\|', query)[1].strip()

I want to know what re has used to separate the string (&, | or ^). How can i do that?

Upvotes: 1

Views: 81

Answers (1)

jamylak
jamylak

Reputation: 133634

If you group the regex it will return the items it split by for every second item.

>>> query
'&foo^bar'
>>> re.split(r'(&|\^|)', query)
['', '&', 'foo', '^', 'bar']

Upvotes: 3

Related Questions