Python:在最后一个小写字母处拆分字符串

时间:2015-03-13 11:29:21

标签: python regex string

我想在字符串中的最后一个小写字母处拆分一个字符串。字符串将始终至少包含一个小写字母。在最后一个小写字母后面总会有一个空格。字符串中可能有数字。

这是我提出的最佳选择:

s = 'Arrived at location NEWARK NJ US'
for i, c in enumerate(reversed(s)):
    if c.islower():
        d = len(s) - i
        break
a = s[:d]
b = s[d+1:]

是否有更简单或更pythonic的方式?也许是一个可读的正则表达式?

2 个答案:

答案 0 :(得分:4)

通过re.split

>>> s = 'Arrived at location NEWARK NJ US'
>>> re.split(r'(?<=[a-z])\s+(?=[^a-z]*$)', s)
['Arrived at location', 'NEWARK NJ US']
  • (?<=[a-z])照看小写字母。
  • \s+然后匹配以下一个或多个空格字符
  • 仅限
  • 后面跟(?=..)
  • [^a-z]*任何字符,但不是小写字母,零次或多次。
  • $紧接着是该行的结尾。所以它匹配紧跟在最后一个小写字母后面的空格。

>>> s = "Arrived at location NEWARK NJ US"
>>> part1, part2 = re.findall(r'(.*[a-z])\s+(.*)', s)[0]
>>> part1
'Arrived at location'
>>> part2
'NEWARK NJ US'

>>> s = "Arrived at location NEWARK NJ US"
>>> part1, part2 = re.search(r'(.*[a-z])\s+(.*)', s).groups()
>>> part1
'Arrived at location'
>>> part2
'NEWARK NJ US'

如果分隔符存在于捕获组中,则re.split将返回分隔符。

>>> part1, part2 = [i for i in re.split(r'(.*[a-z])\s+', s) if not i == '']
>>> part1
'Arrived at location'
>>> part2
'NEWARK NJ US'

答案 1 :(得分:4)

怎么样:

s = 'Arrived at location NEWARK NJ US'
pos = re.search("[a-z]", s[::-1]).start()
a, b = s[:-pos], s[1 - pos:]

ab的结果:

Arrived at location
NEWARK NJ US

想法是通过搜索反向字符串找到最后一个小写字母的位置。

如果您希望在最后一个小写字母后面有多个空格:

start, end = re.search("\s+[a-z]", s[::-1]).span()
a, b = s[:1 - end], s[-start:]