0

I have used regular expression module in order to print the students name from the following text however in the code when I define my name regex it prints the word student only how do I fix it

import re
    list = '''Student name - Shaurya Ronak Ajmera
    Age - 7 years
    Std - 2nd
    Parents name - Ronak and Shital Ajmera
    Phone no - ******************
    Address - ***********
    Knows gujarati - can speak and understand. Reads and writes small sentences.
    Batch - 11 to 12:30 am'''
    numberRegex = re.compile(r'\d{10}')
    nameRegex = re.compile(r'(student)\.*[A-Z a-z]+',re.I)
    mo = nameRegex.findall(list)
    print(mo)
    no = numberRegex.findall(list)
    print(no)

1 Answers1

0

You can repeatedly match all the names after student name - and capture that part in a group instead.

You can omit A-Z and leave a-z in the character class, as re.I makes the match case insensitive.

student name - ([a-z]+(?: [a-z]+)*)

See a Python demo

Example

nameRegex = re.compile(r'student name - ([a-z]+(?: [a-z]+)*)', re.I)
The fourth bird
  • 154,723
  • 16
  • 55
  • 70