我需要在正则表达式搜索之前包含'r'吗?

2024-03-29 12:27:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在分隔符上拆分术语。我想把数字写成index,名字写成name

我的条件:

The Beehive
12. Bar 821
13. Natives Bar
14. Last Call Bar
15. Scarlet Lounge
16. Linden Room
17. Rooftop 25

我使用的代码是:

terms = ['The Beehive', '12. Bar 821', '13. Natives Bar', '14. Last Call Bar', '15. Scarlet Lounge', '16. Linden Room', '17. Rooftop 25']

delim = re.match('\d+\. ', terms)

if delim is None:
    print(delim)
else:
     index = index[:delim.end()]
     name = index[delim.end():]

这无法捕获分割。我已经通过打印delim进行了测试,但它与任何东西都不匹配


Tags: thenameindexbarcallroomlastterms
2条回答

您使用的是列表而不是字符串

import re
terms = ['The Beehive', '12. Bar 821', '13. Natives Bar', '14. Last Call Bar', '15. Scarlet Lounge', '16. Linden Room', '17. Rooftop 25']

delim = re.compile('\d+\.')
for term in terms:
    match = delim.search(term)
    if match:
        print(term[:match.end()]) #index
        print(term[match.end():]) #name

^{}函数只接受单个字符串,因此必须分别迭代terms

>>> for term in terms:
...     match = re.match(r'^(?P<index>(\d+\. )?)(?P<name>.*)$', term)  # Return a match object which contains the named groups.
...     index, _, name = match.groups()  # Unpack the groups.
...     # index = match.group('index')
...     # name = match.group('name')
...     print(index, name)
... 
 The Beehive
12.  Bar 821
13.  Natives Bar
14.  Last Call Bar
15.  Scarlet Lounge
16.  Linden Room
17.  Rooftop 25

还要注意在正则表达式中使用groups,它返回一个带有命名匹配的Group对象

关于是否使用r''前缀,请看this question或文档摘录:

The r prefix, making the literal a raw string literal, is needed […] because escape sequences in a normal “cooked” string literal that are not recognized by Python, as opposed to regular expressions, now result in a DeprecationWarning and will eventually become a SyntaxError. See The Backslash Plague.

相关问题 更多 >