Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我需要在正则表达式搜索之前包含'r'吗?_Python_Regex - Fatal编程技术网

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

Python 我需要在正则表达式搜索之前包含'r'吗?,python,regex,Python,Regex,我想在分隔符上拆分术语。我想把数字写为索引,名字写为名称 我的条件: 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 Lou

我想在分隔符上拆分术语。我想把数字写为
索引
,名字写为
名称

我的条件:

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对其进行了测试,但它与任何内容都不匹配。

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

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

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

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
该函数只接受单个字符串,因此您必须分别迭代
术语

>>> 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
>>对于术语:
...     match=re.match(r'^(?P(\d+\)?)(?P.*)$,term)\返回包含命名组的匹配对象。
...     索引,35;,name=match.groups()#解压缩组。
...     # index=match.group('index')
...     # name=match.group('name')
...     打印(索引、名称)
... 
蜂巢
12酒吧821
13本地人酒吧
14最后呼叫栏
15猩红酒廊
16菩提树室
17天台25
还要注意在正则表达式中使用的,它返回具有命名匹配项的对象

关于是否使用
r''
前缀,请查看以下文档:

需要r前缀,使文本成为原始字符串文本[…],因为Python无法识别的普通“煮熟”字符串文本中的转义序列(与正则表达式相反)现在会导致
弃用警告
,并最终成为
语法错误
。看

该函数只接受单个字符串,因此您必须分别迭代
术语

>>> 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
>>对于术语:
...     match=re.match(r'^(?P(\d+\)?)(?P.*)$,term)\返回包含命名组的匹配对象。
...     索引,35;,name=match.groups()#解压缩组。
...     # index=match.group('index')
...     # name=match.group('name')
...     打印(索引、名称)
... 
蜂巢
12酒吧821
13本地人酒吧
14最后呼叫栏
15猩红酒廊
16菩提树室
17天台25
还要注意在正则表达式中使用的,它返回具有命名匹配项的对象

关于是否使用
r''
前缀,请查看以下文档:

需要r前缀,使文本成为原始字符串文本[…],因为Python无法识别的普通“煮熟”字符串文本中的转义序列(与正则表达式相反)现在会导致
弃用警告
,并最终成为
语法错误
。看


您确定在
delim=re.match('\d+\.',terms)
处没有收到任何错误吗
terms
是一个列表而不是字符串您确定在
delim=re.match('\d+\',terms)
中没有收到任何错误<代码>术语是一个列表而不是字符串我的文本字符串有点奇怪。它不匹配,因为空格实际上是一个不可见字符
~
。这起作用了,除了它不是一个瓷砖外。它在我的文本编辑器中显示为灰色,我无法将其复制并粘贴到stack exchange。当我在编辑器外复制/粘贴时,它看起来什么都没有。我的文本字符串有点奇怪。它不匹配,因为空格实际上是一个不可见字符
~
。这起作用了,除了它不是一个瓷砖外。它在我的文本编辑器中显示为灰色,我无法将其复制并粘贴到stack exchange。当我在编辑器外复制/粘贴时,它显示为“无”