Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string by capital letter but ignore AAA Python Regex

My regex:

vendor = "MyNameIsJoe. I'mWorkerInAAAinc."
ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor)

To split strings by capital letter, for example:

'MyNameIsJoe. I'mWorkerInAAAinc' becomes 'My Name Is Joe. I'm Worker In AAA inc.'


1 Answers

You can use re.findall() in order to find the expected words rather than splitting:

In [46]: ' '.join(re.findall(r'[A-Z]?[^A-Z\s]+|[A-Z]+', vendor))
Out[46]: "My Name Is Joe. I'm Worker In AAA inc."

Note that this opting [A-Z]+ will match the AAA which means any combinations of uppercase letters with length <1 if you don't want this you can simple just use AAA.

like image 75
Mazdak Avatar answered Feb 06 '26 15:02

Mazdak