仅匹配最后一个下划线后具有字符串的字符串

时间:2014-05-12 18:14:35

标签: python regex

我正在尝试将字符串与下划线匹配,整个字符串中都有下划线但我想匹配最后一个下划线后面有字符串的字符串:让我举个例子:

s = "hello_world"
s1 = "hello_world_foo"
s2 = "hello_world_foo_boo"

在我的情况下,想要捕获 s1 s2

我开始关注,但我无法确定如何匹配以捕获hello_world下划线后的字符串。

rgx = re.compile(ur'(?P<firstpart>\w+)[_]+(?P<secondpart>\w+)$', re.I | re.U)

2 个答案:

答案 0 :(得分:1)

试试这个:

reobj = re.compile("^(?P<firstpart>[a-z]+)_(?P<secondpart>[a-z]+)_(?P<lastpart>.*?)$", re.IGNORECASE)
result = reobj.findall(subject)

正则表达式解释

^(?P<firstpart>[a-z]+)_(?P<secondpart>[a-z]+)_(?P<lastpart>.*?)$

Options: case insensitive

Assert position at the beginning of the string «^»
Match the regular expression below and capture its match into backreference with name “firstpart” «(?P<firstpart>[a-z]+)»
   Match a single character in the range between “a” and “z” «[a-z]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “_” literally «_»
Match the regular expression below and capture its match into backreference with name “secondpart” «(?P<secondpart>[a-z]+)»
   Match a single character in the range between “a” and “z” «[a-z]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “_” literally «_»
Match the regular expression below and capture its match into backreference with name “lastpart” «(?P<lastpart>.*?)»
   Match any single character that is not a line break character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

答案 1 :(得分:0)

如果我理解你的要求(你想要将字符串与多个下划线匹配并跟随文字)

rgx = re.compile(ur'(?P<firstpart>\w+)[_]+(?P<secondpart>\w+)_[^_]+$', re.I | re.U)