Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string on first occurrence of separator only

The POS tagger that I use processes the following string

3+2

as shown below.

3/num++/sign+2/num

I'd like to split this result as follows using python.

['3/num', '+/sign', '2/num']         

How can I do that?

like image 768
Bryan Avatar asked Mar 07 '23 07:03

Bryan


1 Answers

Use re.split -

>>> import re
>>> re.split(r'(?<!\+)\+', '3/num++/sign+2/num')
['3/num', '+/sign', '2/num']

The regex pattern will split on a + sign as long as no other + precedes it.

(?<!   # negative lookbehind
\+     # plus sign
)     
\+     # plus sign

Note that lookbehinds (in general) do not support varying length patterns.

like image 84
cs95 Avatar answered Mar 10 '23 11:03

cs95