Skip to content Skip to sidebar Skip to footer

Capture All Repetitions Of A Group Using Python Regular Expression

I have an input of the following format: : ... # where ... can represent one or more strings. Here is an example: 1: foo b

Solution 1:

Non-regex solution:

>>> s = '1: foo bar baz # This is an example'>>> a, _, b = s.partition(':')
>>> [int(a)] + b.partition('#')[0].split()
[1, 'foo', 'bar', 'baz']

Solution 2:

You can probably make it a lot clearer with simple string manipulation.

my_string = '1: foo bar baz'
num_string, word_string = my_string.split(':')
num = int(num_string)
words = word_string.strip().split(' ')

print(num)
print(words)

Output:

# num = 1# words = ['foo', 'bar', 'baz']

Solution 3:

The trick here is to use lookeaheads: let's find either digits (followed by a colon) or words (followed by letters/spaces and a hash):

s = "1: foo bar baz # This is an example"print re.findall(r'\d+(?=:)|\w+(?=[\w\s]*#)', s)
# ['1', 'foo', 'bar', 'baz']

The only thing that remains is to convert "1" to an int - but you can't do that with regexp.

Post a Comment for "Capture All Repetitions Of A Group Using Python Regular Expression"