Likai Ren
Likai Ren

Reputation: 125

Python Regex - group match

import re
details = '(2,5  cr / K / M)'
m = re.match(r'\((.*?)\w+cr\w/\w(.)\w/\w(.)\)', details)

credit = m.group(0)
state = m.group(1)
grade = m.group(2)


course = {'credit': credit, 'state': state, 'grade': grade}

print course

As this snippet shows, I want to get (? cr / ? / ?), but it doesn't work.

Upvotes: 1

Views: 76

Answers (3)

heemayl
heemayl

Reputation: 42027

You can use \w[^\s)]* and then remove any unwanted value:

>>> s = '(2,5  cr / K / M)'

>>> re.findall(r'\w[^\s)]*', s)
['2,5', 'cr', 'K', 'M']

Then you just need assign values discarding cr at index 1.

>>> s = '(2,5  cr / K / M)'
>>> out = re.findall(r'\w[^\s)]*', s)
>>> credit = out[0]
>>> state = out[2]
>>> grade = out[3]
>>> course = {'credit': credit, 'state': state, 'grade': grade}
>>> course
{'state': 'K', 'grade': 'M', 'credit': '2,5'}

Upvotes: 0

ryanmc
ryanmc

Reputation: 1881

Looks ilke you just confused w and s.. May not fit all edge cases:

import re

details = '(2,5  cr / K / M)'
pattern = re.compile(r'^\(([0-9],[0-9]).*/\s([A-Z])\s/\s([A-Z])\)')
m = pattern.match(details)

credit = m.group(1)
state = m.group(2)
grade = m.group(3)

course = {'credit': credit, 'state': state, 'grade': grade}

print course

Upvotes: 0

aghast
aghast

Reputation: 15310

Change:

m = re.match(r'\((.*?)\w+cr\w/\w(.)\w/\w(.)\)', details)

To:

m = re.match(r'\((\S+)\s+cr\s+/\s+(\S)\s+/\s+(\S)\)', details)

Where \s is "whitespace" and \S is "anything-but-whitespace".

Upvotes: 1

Related Questions