Is there a way to combine multiple strings using Regex?

Viewed 176

Having an issue with Regex and not really understanding its usefulness right now. Trying to extrapolate data from a file. file consists of first name, last name, grade

File:

Peter Jenkins: A  
Robert Right: B  
Kim Long: C  
Jim Jim: B

Opening file code:

##Regex Code  r'([A-Za-z]+)(: B)
regcode = r'([A-Za-z]+)(: B)'

answer=re.findall(regcode,file)
return answer

The expected result is first name last name. The given result is last name and letter grade. How do I just get the first name and last name for all B grades?

4 Answers

Since you must use regex for this task, here's a simple regex solution that returns the full name:

'(.*): B'

Which works in this case because:

(.*) returns all text up to a match of : B

Click here to see my test and matching output. I recommend this site for your regex testing needs.

You can do it without regex:

students = '''Peter Jenkins: A
Robert Right: B
Kim Long: C
Jim Jim: B'''
    
for x in students.split('\n'):
    string = x.split(': ')
    if string[1] == 'B':
        print(string[0])

# Robert Right
# Jim Jim

or

[x[0:-3] for x in students.split('\n') if x[-1] == 'B']

If a regex solution is required (I perosnally like the solution of Roman Zhak more), put inside a group what you are interested in, i.e. the first name and the second name. Follows colon and B:

import re

file = """
Peter Jenkins: A  
Robert Right: B  
Kim Long: C  
Jim Jim: B
"""

regcode = r'([A-Za-z]+) ([A-Za-z]+): B'
answer=re.findall(regcode,file,re.)
print(answer) # [('Robert', 'Right'), ('Jim', 'Jim')]

Add a capturing group ('()') to your expression. Everything outside the group will be ignored, even if it matches the expression.

re.findall('(\w+\s+\w+):\s+B', file)
#['Robert Right', 'Jim Jim']

'\w' is any alphanumeric character, '\s' is any space-like character.

You can add two groups, one for the first name and one for the last name:

re.findall('(\w+)\s+(\w+):\s+B', data)
#[('Robert', 'Right'), ('Jim', 'Jim')]

The latter will not work if there are more than two names on one line.

Related