fork download
  1. import re
  2. texts = ['GPA 3.75/4.00','GPA 3.75/4.0','GPA 3.75 /4.0','GPA 3.75 / 4.0','GPA 3.7 / 4 aaa','Major GPA: 3.6/4.0','GPA 3.1','GPA 3','some text GPA 3','3.25/4.0 GPA','some text 3.26 / 4.0 GPA','Minor in Art and Technology - 3.5/4.0 GPA aaaa','Minor in Art and Technology - 3.5 / 4.3 GPA aaaa']
  3. rx = re.compile(r'(\bgpa[ :]+)?(\d+(?:\.\d+)?)[/\d. ]{0,6}(?(1)| *gpa\b)', re.I)
  4. for text in texts:
  5. m = rx.search(text)
  6. if m:
  7. print(text, "=>", m.group(2), sep=" ")
Success #stdin #stdout 0.02s 9808KB
stdin
Standard input is empty
stdout
GPA 3.75/4.00 => 3.75
GPA 3.75/4.0 => 3.75
GPA 3.75 /4.0 => 3.75
GPA 3.75 / 4.0 => 3.75
GPA 3.7 / 4      aaa => 3.7
Major GPA: 3.6/4.0 => 3.6
GPA 3.1 => 3.1
GPA 3 => 3
some text GPA 3 => 3
3.25/4.0 GPA => 3.25
some text 3.26 / 4.0 GPA => 3.26
Minor in Art and Technology - 3.5/4.0 GPA aaaa => 3.5
Minor in Art and Technology - 3.5 / 4.3 GPA aaaa => 3.5