Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Regex Simple Split - Empty at first index

Tags:

python

regex

I have a String that looks like

test = '20170125NBCNightlyNews'

I am trying to split it into two parts, the digits, and the name. The format will always be [date][show] the date is stripped of format and is digit only in the direction of YYYYMMDD (dont think that matters)

I am trying to use re. I have a working version by writing.

re.split('(\d+)',test)

Simple enough, this gives me the values I need in a list.

['', '20170125', 'NBCNightlyNews']

However, as you will note, there is an empty string in the first position. I could theoretically just ignore it, but I want to learn why it is there in the first place, and if/how I can avoid it.

I also tried telling it to match the begininning of the string as well, and got the same results.

>>> re.split('(^\d+)',test)
['', '20170125', 'NBCNightlyNews']
>>> re.split('^(\d+)',test)
['', '20170125', 'NBCNightlyNews']
>>>

Does anyone have any input as to why this is there / how I can avoid the empty string?

like image 951
Busturdust Avatar asked Nov 30 '25 11:11

Busturdust


1 Answers

Other answers have explained why what you're doing does what it does, but if you have a constant format for the date, there is no reason to abuse a re.split to parse this data:

test[:8], test[8:]

Will split your strings just fine.

like image 174
TemporalWolf Avatar answered Dec 02 '25 05:12

TemporalWolf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!