简体   繁体   中英

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.

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".

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

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'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM