2

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
  • 1
    Do you need regex? I think simple split and some filter / complrehension should fo the job Commented Sep 7, 2020 at 19:35
  • unfortunately regex is needed or this would not be an issue Commented Sep 7, 2020 at 19:36
  • Ok, why do you need regex? There are easier (IMO) solutions.. But the regex way: r'([A-Za-z]+)(: B)' matches a word (in 1st group) followed by : B (matched to the second group). Just match one more word and it should work Commented Sep 7, 2020 at 19:38
  • Thanks for the response. Its for a class Commented Sep 8, 2020 at 2:29

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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']

Comments

0

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')]

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.