0

I want to ask some question Regular Expression in Python.

In the variable nihao, there is a string like the following:

Committee Members: Prof.A (Supervisor)

I want to extract Prof.A(only the Prof.A, eliminate all the \t or other white spaces) between

Committee Members:

and

(Supervisor)in python language.

How should I write the regex?

Thanks.

2
  • "Is it correct to write like this?" Did you try it? Or should we try for you? Commented Nov 10, 2014 at 13:03
  • I try it... it seemd failed... The actual point is that I do not know how to write the logic.. Commented Nov 10, 2014 at 13:41

2 Answers 2

3

Just a couple of edits:

  • You had Member instead of Members.
  • You have to escape the parentheses around Supervisor
  • ([\w|.|\s]+) would be a better capturing group.

Committee\sMembers:\s+([\w.\s]+)\s\(Supervisor\)

Look here: http://regex101.com/r/pS9bQ9/4

Note: Next time, regex101.com is a good website to test your regex's

1
  • Remove unessary | in the character class. \w, \s and . are already ORed inside character class. Commented Nov 10, 2014 at 13:52
0

Expanding @VincentBeltman's regex a bit, it can be done like this as well -

 # Committee\s+Members:\s*(\w[\w.\s]*?)\s*\(Supervisor\)

 Committee \s+ Members:
 \s* 
 ( \w [\w.\s]*? )         # (1)
 \s* 
 \(Supervisor\)

Your Answer

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