如何在python中组合正则表达式模式查找和位置查找?

时间:2018-04-16 05:03:29

标签: python regex string pattern-matching

我是正则表达的新手,所以请耐心等待。假设我有两个字符串:

string_1 = 'aAbcDeF801020J00100870'
string_2 = 'aAbF801020J00100870'

使用 re 模块,我可以匹配字符串的非数字第1部分

pattern = re.compile("^[aA-zZ]*")

print(pattern.match(string_1).group(0))
print(pattern.match(string_2).group(0))

>> aAbcDeF
>> aAbF

但是如何获得跟随匹配模式的6个数字字符的序列?即。

801020

尝试以下方法:

pattern = re.compile("^[aA-zZ]*......")


print(pattern.match(string_1).group(0))
print(pattern.match(string_2).group(0))

给了我:

>> aAbcDeF801020
>> aAbF801020

哪个更近,但不完全相同。我希望在单次模式查找中执行此操作,而在上次尝试结束时添加[-6:]。

提前致谢

2 个答案:

答案 0 :(得分:1)

你可以尝试:

^([aA-zZ]*)(\d{6})

像这样:

string_1 = 'aAbcDeF801020J00100870'
string_2 = 'aAbF801020J00100870'

pattern = re.compile("^([aA-zZ]*)(\d{6})")

print("Full match:", pattern.match(string_1).group(0))

print(pattern.match(string_1).group(1))
print(pattern.match(string_2).group(1))

print(pattern.match(string_1).group(2))
print(pattern.match(string_2).group(2))

给你输出:

Full match: aAbcDeF801020
aAbcDeF
aAbF
801020
801020

group(1)包含要匹配的文字,group(2)包含6位数字 如果您愿意,可以使用group(0)

获取文字以及6位数字

<强> Here's a fully functioning demo at regex101.com

正如您在页面上提供的匹配信息中所看到的,找到的完整匹配是字母和6位数字。

注意 - 此外,如果您想要找到此模式的所有次出现,您可以使用re.findall(),如下所示:

string_1 = 'aAbcDeF801020J00100870'
string_2 = 'aAbF801020J00100870'

print(re.findall('([aA-zZ]*)(\d{6})', string_1))
print(re.findall('([aA-zZ]*)(\d{6})', string_2))

这会产生一系列元组:

[('aAbcDeF', '801020'), ('J', '001008')]
[('aAbF', '801020'), ('J', '001008')]

答案 1 :(得分:0)

您可以使用^([a-zA-Z])*([0-9]+)

pattern = re.compile("^([a-zA-Z])*([0-9]+)")

print(pattern.match(string_1).groups())
print(pattern.match(string_2).groups())

输出:

('aAbcDeF', '801020')
('aAbF', '801020')

解释

^                           // asserts position at start of the string
    (                       // start of matching group 1
        [a-zA-Z]            // matches a character present in the list
        *                   // matches between zero and unlimited times (greedy)
    )                       // end of matching group 1
    (                       // start of matching group 2
        [0-9]+              // matches any number between 1 and unlimited times
    )                       // end of matching group 2