Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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/1/typo3/2.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正则表达式匹配_Python_Regex - Fatal编程技术网

搜索字符串意外部分上的Python正则表达式匹配

搜索字符串意外部分上的Python正则表达式匹配,python,regex,Python,Regex,我正在尝试使用正则表达式(Python 2.7;IPython QTConsole)解析页面。该页面是从我使用urllib2 >>> import re >>> Z = '[A-Z]{2}Z[0-9]{3}.*?\\$\\$' >>> snippet = re.search(Z, page, re.DOTALL) >>> snippet = snippet.group() # Only including the first

我正在尝试使用正则表达式(Python 2.7;IPython QTConsole)解析页面。该页面是从我使用
urllib2

>>> import re
>>> Z = '[A-Z]{2}Z[0-9]{3}.*?\\$\\$'
>>> snippet = re.search(Z, page, re.DOTALL)
>>> snippet = snippet.group() # Only including the first part for brevity.
'PZZ570-122200-\nPOINT ARENA TO POINT REYES 10 TO 60 NM OFFSHORE-\n249 AM PDT FRI SEP 12 2014\n.TODAY...SW WINDS 5 KT. WIND WAVES 2 FT OR LESS.\nNW SWELL 3 TO 5 FT AT 12 SECONDS. PATCHY FOG IN THE MORNING.\n.TONIGHT...W WINDS 10 KT. WIND WAVES 2 FT OR LESS.'
我想搜索后跟句点的换行符。我想得到第一次和第二次出现如下。目标是解析第一个和第二个(以及后续)分隔符之间的信息。我知道我可以四处看看,但我很难让前瞻变得贪婪。此外,我不明白为什么下面的方法不起作用

>>> pat = r"\n\."
>>> s = re.search(pat, snippet.group(), re.DOTALL)
>>> e = re.search(pat, snippet.group()[s.end():], re.DOTALL)
上面的
s
可以工作,但是
e
得到了一个奇怪的结果

>>> [s.group(), s.start(), e.group(), e.end()]
['\n.', 90, '\n.', 110]

>>> snippet.group()[s.start():e.end()]
'\n.TODAY...SW WINDS 5'

>>> snippet.group()[e.start():e.end()]
' 5'
我猜
snippet.group()
中隐藏了一些格式?如果是这种情况,那么奇怪的是,有些新行是显式的,好像
snippet.group()
是原始的,而另一些是隐藏的。为什么
e.group()
snippet.group()[e.start():e.end()]
不同

如果这个问题已经得到解决,我很抱歉。我找不到任何相关的东西


非常感谢。

要在python中拆分字符串,它可能更易于使用或删除

e、 g:


您可能会更幸运地使用
re.split()
来实现您声明的目标,它就像一个符咒。非常感谢。我仍然对上面的行为感到好奇。不知道,我必须花很多时间调试:(完全不值得:)。谢谢如果我弄明白了,我会在这里贴些东西。
"1\n.2\n.3".split("\n.")