如何捕获字符串中的特定单词?

时间:2014-07-18 13:14:17

标签: python python-2.7

我想只捕捉各个部门的名字和学生人数:

line = "The Biology department averages 32 students/class" 

我已经尝试re.search(r"\s\D+\d", line),但它无法正常工作。

3 个答案:

答案 0 :(得分:2)

使用lookahead

,比基督徒的答案更明确一些方法
>>> s = "The Biology department averages 32 students/class"
>>> dept = r'\s+(\w+)\s+(?=department)'
>>> students = r'\s+(\d+)\s+(?=students)'
>>> re.findall(dept, s),re.findall(students,s)
(['Biology'], ['32'])

答案 1 :(得分:1)

In [3]: department, students = re.search(r"(?:The\s+)?([A-Z]\w*).*\s+(\d+)\s+", line).groups()

In [4]: print department, students
Biology 32

答案 2 :(得分:1)

更清晰的方法,LOL

# encoding: UTF-8
import re
s = 'The Biology department averages 32 students/class'
pattern1 = re.compile(r'.*?The (.*?) department')
match1 = pattern1.match(s)
if match1:
    print match1.group(1)
pattern2 = re.compile(r'.*? (\d.?) students')
match2 = pattern2.match(s)
if match2:
    print match2.group(1)