1

I am working on a regex problem from coursera and it has provided a data file of names followed by ": grade". We have to find and return the names of all the students who have a B. What I did was use this regex code

Bstudents = re.findall("(/w )*: B", grades)   #grades is the name of the variable with the names+grades. 

There are supposed to be 16 students who have the grade B, but Instead of their names I am getting 16 blank strings.

here is the list:

Ronald Mayr: A
Bell Kassulke: B
Jacqueline Rupp: A 
Alexander Zeller: C
Valentina Denk: C 
Simon Loidl: B 
Elias Jovanovic: B 
Stefanie Weninger: A 
Fabian Peer: C 
Hakim Botros: B
Emilie Lorentsen: B
Herman Karlsen: C
Nathalie Delacruz: C
Casey Hartman: C
Lily Walker : A
Gerard Wang: C
Tony Mcdowell: C
Jake Wood: B
Fatemeh Akhtar: B
Kim Weston: B
Nicholas Beatty: A
Kirsten Williams: C
Vaishali Surana: C
Coby Mccormack: C
Yasmin Dar: B
Romy Donnelly: A
Viswamitra Upandhye: B
Kendrick Hilpert: A
Killian Kaufman: B
Elwood Page: B
Mukti Patel: A
Emily Lesch: C
Elodie Booker: B
Jedd Kim: A
Annabel Davies: A
Adnan Chen: B
Jonathan Berg: C
Hank Spinka: B
Agnes Schneider: C
Kimberly Green: A
Lola-Rose Coates: C
Rose Christiansen: C
Shirley Hintz: C
Hannah Bayer: B
3
  • 2
    Use a tool like regex101.com - it allows you to [specify Python as your regEx engine and] tweak your pattern and see in real time what does/doesn't match. Commented Aug 5, 2021 at 13:48
  • Use the pattern "(/w ).*: B" Commented Aug 5, 2021 at 13:48
  • /w matches literally a slash and a double-u… Commented Aug 5, 2021 at 13:49

1 Answer 1

2

You can use positive lookahead: [\w ]+(?=: B)

See Demo

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

3 Comments

was the problem that I didn't use () to identify ": B" or was it because I used "*" instead of "+"?
Almost none of them, see the link.
We know every grade have name of a student before it so we can use + that means one or more character instead of * that means zero or more

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.