henkimon
henkimon

Reputation: 1481

String splitting in Python using regex

I'm trying to split a string in Python so that I get everything before a certain regex.

example string: "Some.File.Num10.example.txt"

I need everything before this part: "Num10", regex: r'Num\d\d' (the number will vary and possibly what comes after).

Any ideas on how to do this?

Upvotes: 11

Views: 18844

Answers (3)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799560

You can use Python's re.split()

import re

my_str = "This is a string."

re.split("\W+", my_str)

['This', 'is', 'a', 'string', '']

Upvotes: 5

varunl
varunl

Reputation: 20269

>>> import re
>>> s = "Some.File.Num10.example.txt"
>>> p = re.compile("Num\d{2}")
>>> match = p.search(s)
>>> s[:match.start()]
'Some.File.'

This would be more efficient that doing a split because search doesn't have to scan the whole string. It breaks on the first match. In your example it wouldn't make a different as the strings are short but in case your string is very long and you know that the match is going to be in the beginning, then this approach would be faster.

I just wrote a small program to profile search() and split() and confirmed the above assertion.

Upvotes: 13

jamylak
jamylak

Reputation: 133764

>>> import re
>>> text = "Some.File.Num10.example.txt"
>>> re.split(r'Num\d{2}',text)[0]
'Some.File.'

Upvotes: 10

Related Questions