正则表达式捕获最多2位数字和昏迷,如果后跟另一个单词和数字

时间:2013-12-10 14:09:50

标签: python regex

我需要一个正则表达式,匹配并在满足条件时从字符串返回2个数字

  1. 只有最多2位且不大于29的数字(可能包含小数的情况 - 最多2位数加上1位小数)

  2. 他们必须介于其中一个字符y+之间,并且在第二个数字后面加上'house'一词

  3. 然后捕获两个数字

    以下字符串:

    8 y 13 houses, 13 y 8 houses, 13 y 13 houses, 8 y 8 houses, 120 y 8 houses, 8 y 120 houses, 13,5 y 8 houses, 13,5 y 120 houses
    

    我会得到

    8 and 13 / 13 and 8 / 13 and 13 / 8,8 / 13,5 and 5
    

    我正在尝试这个

    \b([0-9][0-9]?)\s[y|\+]\s([0-9]{1,2})\shouses\b
    

    但也无法获得','。

2 个答案:

答案 0 :(得分:4)

您如果要将可选的十进制值与可选组匹配:

re.compile(r"\b([1-2]?\d(?:,\d)?)\s[y+]\s([1-2]?\d(?:,\d)?)\shouses\b")

其中(?:,[0-9])?将匹配逗号,后跟数字(如果存在)。请注意,我将数字匹配限制在0到29之间的值;首先匹配可选的12,然后是0-9

演示:

>>> import re
>>> demo = '8 y 13 houses, 13 y 8 houses, 13 y 13 houses, 8 y 8 houses, 120 y 8 houses, 8 y 120 houses, 13,5 y 8 houses, 13,5 y 120 houses'
>>> pattern = re.compile(r"\b([1-2]?\d(?:,\d)?)\s[y+]\s([1-2]?\d(?:,\d)?)\shouses\b")
>>> pattern.findall(demo)
[('8', '13'), ('13', '8'), ('13', '13'), ('8', '8'), ('13,5', '8')]

答案 1 :(得分:1)

这是一个尝试:

#! /usr/bin/env python

import re

str = '8 y 13 houses, 13 y 8 houses, 13 y 13 houses, 8 y 8 houses, 120 y 8 houses, 8 y 120 houses, 13,5 y 8 houses, 13,5 y 120 houses'

regex = r'''
\b (
    [012]?     # number may go up to 29, so could have a leading 0, 1, or 2
    [0-9]      # but there must be at least one digit 0-9 here
    (,[0-9])?  # and the digits might be followed by one decimal point
)
\s* [y+] \s*   # must be a 'y' or '+' in between
(
    [012]?     # followed by another 0-29
    [0-9]
    (,[0-9])?  # and an optional decimal point
)
\s* houses \b  # followed by the word "houses"
'''

for match in re.finditer(regex, str, re.VERBOSE):
    print "found: %s and %s" % (match.group(1), match.group(3))

演示:

$ python pyregex.py 
found: 8 and 13
found: 13 and 8
found: 13 and 13
found: 8 and 8
found: 13,5 and 8

当该正则表达式与输入中的字符串匹配时,第一个数字将位于match.group(1),第二个数字将位于match.group(3)