Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - PCRE_NOTEMPTY

Tags:

python

regex

This is a very basic question, but I am stuck, so any help would be appreciated.

import re
re.split(r'(\d|\W|\_)', '  ab2c d_ef')
['', ' ', '', ' ', 'ab', '2', 'c', ' ', 'd', '_', 'ef']

How do I get rid of the empty '' match? I do need the space character. I want my output to look like below:

[' ', ' ', 'ab', '2', 'c', ' ', 'd', '_', 'ef']
like image 776
user3198821 Avatar asked Dec 03 '25 08:12

user3198821


1 Answers

Use re.findall instead of re.split:

>>> import re
>>> re.findall(r'[a-z]+|\d+|_|\s', '  ab2c d_ef')
[' ', ' ', 'ab', '2', 'c', ' ', 'd', '_', 'ef']

You don't need to escape _. It does not have special meaning in regular expression.

like image 76
falsetru Avatar answered Dec 05 '25 23:12

falsetru