Is there a way I can get selective data from strings like these?

Viewed 46

I am working on a program which fetches strings like these:

s = 'GPA: 3.4 GRE: 317 Round: Round 2 | West'
d = 'GPA: 3.7 GRE: 328 Round: Round 3 | Singapore'
a = 'GPA: undergrad: 3.68 grad: DPT 3.32 GMAT Waiver Round: Round 3'
c = 'GPA: 3.2 GMAT: 750 Round: Rolling Admissions | NY'

Until now I was just fetching the number of rounds using re expressions. But for some exceptions like c, I need to get it to return 'rolling admissions' as well. Is there a way to get data between round and the pipe sign?

I had been doing like this:

R = re.findall('\S*Round ([a-zA-Z0-9]+)', d)
print(''.join(R))
2 Answers

The regex below gets the value between the two delimters Round: and |

import re

c = 'GPA: 3.2 GMAT: 750 Round: Rolling Admissions | NY'
R = re.findall('Round\: (.*?) \|', c)
print(R) # output: 'Rolling Admissions'

Edit: Noted that there are a few instance where the pipe sign isn't available. Using the following will search the string after Round: and before | or till the end of the string

import re

s = 'GPA: 3.4 GRE: 317 Round: Round 2 | West'
d = 'GPA: 3.7 GRE: 328 Round: Round 3 | Singapore'
a = 'GPA: undergrad: 3.68 grad: DPT 3.32 GMAT Waiver Round: Round 3'
c = 'GPA: 3.2 GMAT: 750 Round: Rolling Admissions | NY'

print(re.findall('Round: (.*?) ($|\|)', s)) #output: [('Round 2', '|')]
print(re.findall('Round: (.*?) ($|\|)', d)) #output: [('Round 3', '|')]
print(re.findall('Round: (.*?) ($|\|)', a)) #output: [('Round 3 ', '|')]
print(re.findall('Round: (.*?) ($|\|)', c)) #output: [('Rolling Admissions', '|')]

You will just need to get the first element and that will be your requested value

eg.

value = re.findall('Round: (.*?) ($|\|)', c)) #output: [('Rolling Admissions', '|')]
print(value[0][0]) # since the output is a tuple within a list

here you go

c = 'GPA: 3.2 GMAT: 750 Round: Rolling Admissions | NY'
c.split('|')[0]
Related