Home > Enterprise >  Regex for finding names after number
Regex for finding names after number

Time:10-11

I have string which contains names:

"1. Sachin Deshpande 2. K. Chandra Shekar 3. B. Rupesh 1. Prabhakar Rao 2. Venkata Chary 3. B. Gangadhar"

I need list of names from above string. I have tried (?:[0-9][).]) (\D*) this regex but it gives false results as well. Any help would be appreciated.

above regex also matches with raj sharma 4) mirror ship

I am looking for regex which checks the sequence for number

Output should look like this :

["Sachin Deshpande","K. Chandra Shekar","B. Rupesh","Prabhakar Rao",...]

CodePudding user response:

With:

\d \.\s (\D )(?:\s |$)

Demo

Python demo:

>>> import re 
>>> s="1. Sachin Deshpande 2. K. Chandra Shekar 3. B. Rupesh 1. Prabhakar Rao 2. Venkata Chary 3. B. Gangadhar"
>>> re.findall(r'\d \.\s (\D )(?:\s |$)', s)
['Sachin Deshpande', 'K. Chandra Shekar', 'B. Rupesh', 'Prabhakar Rao', 'Venkata Chary', 'B. Gangadhar']

If you have

"1) Sachin Deshpande 2) K. Chandra Shekar 3) B. Rupesh 1) Prabhakar Rao 2) Venkata Chary 3) B. Gangadhar"

You can use:

\d [.)]\s (\D )(?:\s |$)

Demo

CodePudding user response:

/(?<=[0-9]\)|\. ) (\D*)/gm
There will be a space after it, but I tried my best :P

  • Related