Python regular expressions - how to get all the names in a string?

How to get names from a string as shown below using regex ??

line #1==> 
Elector Name: Surpam Badurubai Elector Name: Madavimaru Elector Name: Madavitannubai 

line #2==>
Elector Name: GEDAM KARNU Elector Name: GEDAM BHEEM BAI Elector Name: Surpam Rajeshwar Rav

I tried

regex = "\s*Elector\'s\sName\:\s([[a-zA-z]*\s[a-zA-z]*\s*[a-zA-z]*]*)\s" 
re.findall(regex, line)

It worked for line 1, but could not get the last name. For line 2, he selects "Surpam Rajeshwar" with the last name, but actually has 3 words in it.

I appreciate if someone can help me with this or offer me another way to get the names. !!

+4
source share
4 answers

You can do this without a regular expression by separating it with Elector Name:, removing the elements from spaces and omitting all empty elements:

ss = ["Elector Name: Surpam Badurubai Elector Name: Madavimaru Elector Name: Madavitannubai",
   "Elector Name: GEDAM KARNU Elector Name: GEDAM BHEEM BAI Elector Name: Surpam Rajeshwar Rav"]
for s in ss:
    print(filter(None, [x.strip() for x in s.split("Elector Name:")]))

See Python demo , output:

['Surpam Badurubai', 'Madavimaru', 'Madavitannubai']
['GEDAM KARNU', 'GEDAM BHEEM BAI', 'Surpam Rajeshwar Rav']

, , :

re.findall(r"Elector Name:\s*(.*?)(?=\s*Elector Name:|$)", s) 

Python

  • Elector Name: -
  • \s* - 0+
  • (.*?) - 1 ( re.findall): 0+, ( re.DOTALL, ),
  • (?=\s*Elector Name:|$) - , 0+ Elector Name: ($) .
+4

, re.split "Elector Name: " ( ), , :

[x for x in re.split("\s*Elector Name:\s*",l1) if x]

:

['GEDAM KARNU', 'GEDAM BHEEM BAI', 'Surpam Rajeshwar Rav']
['Surpam Badurubai', 'Madavimaru', 'Madavitannubai']

, , str.split() str.split():

[x.strip() for x in l1.split("Elector Name:") if x]
+1

, .split() Elector Name:. :

names = line.split('Elector Name:')
for i in names:
    print(i)
+1

Jamie Zawinski:

Some people, faced with a problem, think: "I know, I will use regular expressions." Now they have two problems.

So using python

line = "Elector Name: Surpam Badurubai Elector Name: Madavimaru Elector Name: Madavitannubai"
[name.strip() for name in line.split("Elector Name:") if name != '']
0
source

Source: https://habr.com/ru/post/1688201/


All Articles