Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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_Mapreduce - Fatal编程技术网

Python正则表达式查找多个连续标点

Python正则表达式查找多个连续标点,python,regex,mapreduce,Python,Regex,Mapreduce,我正在通过MapReduce传输纯文本记录,需要检查每个纯文本记录是否有2个或更多连续标点符号。我需要检查的12个符号是:-/\()!“+,”和。 我尝试将此标点符号列表转换为如下数组: 标点符号=[r'-',r'/',r'\\',r'\'(',r'\')',r'!',r'''',r'\+',r'',r'\'',r'\.] 我可以找到带有嵌套for循环的单个字符,例如: for t in test_cases: print t for p in punctuation:

我正在通过MapReduce传输纯文本记录,需要检查每个纯文本记录是否有2个或更多连续标点符号。我需要检查的12个符号是:
-/\()!“+,”和。

我尝试将此标点符号列表转换为如下数组: 标点符号=
[r'-',r'/',r'\\',r'\'(',r'\')',r'!',r'''',r'\+',r'',r'\'',r'\.]

我可以找到带有嵌套for循环的单个字符,例如:

for t in test_cases:
    print t
    for p in punctuation:
        print p
        if re.search(p, t):
            print 'found a match!', p, t
        else:
            print 'no match'
但是,当我测试这个时,找不到单个反斜杠字符,我不知道如何仅获得一行中连续出现2次或更多次的结果。我已经读到我需要使用+符号,但不知道使用这个符号的正确语法

以下是一些测试用例:

The quick '''brown fox
The &&quick brown fox
The quick\brown fox
The quick\\brown fox
The -quick brown// fox
The quick--brown fox
The (quick brown) fox,,,
The quick ++brown fox
The "quick brown" fox
The quick/brown fox
The quick&brown fox
The ""quick"" brown fox
The quick,, brown fox
The quick brown fox…
The quick-brown fox
The ((quick brown fox
The quick brown)) fox
The quick brown fox!!!
The 'quick' brown fox
 1 The quick '''brown fox   => '''
 2 The &&quick brown fox    => &&
 3 The quick\\brown fox     => \\
 4 The -quick brown// fox   => //
 5 The quick--brown fox     => --
 6 The (quick brown) fox,,, => ,,,
 7 The quick ++brown fox    => ++
 8 The ""quick"" brown fox  => ""
 9 The quick,, brown fox    => ,,
10 The quick brown fox...   => ...
11 The ((quick brown fox    => ((
12 The quick brown)) fox    => ))
13 The quick brown fox!!!   => !!!
将其转换为Python列表时如下所示:

test_cases = [
"The quick '''brown fox",
'The &&quick brown fox',
'The quick\\brown fox',
'The quick\\\\brown fox',
'The -quick brown// fox',
'The quick--brown fox',
'The (quick brown) fox,,,',
'The quick ++brown fox',
'The "quick brown" fox',
'The quick/brown fox',
'The quick&brown fox',
'The ""quick"" brown fox',
'The quick,, brown fox',
'The quick brown fox...',
'The quick-brown fox',
'The ((quick brown fox',
'The quick brown)) fox',
'The quick brown fox!!!',
"The 'quick' brown fox" ]

如何使用Python正则表达式识别和报告标点符号在一行中出现两次或两次以上的所有匹配?

您可以在正则表达式中使用
{2}
来匹配字符类的两个连续出现:

>>> regex = re.compile(r'[-/()!"+,\'&]{2}')
>>> [s for s in test_cases if regex.search(s)]
["The quick '''brown fox",
 'The &&quick brown fox',
 'The -quick brown// fox',
 'The quick--brown fox',
 'The (quick brown) fox,,,',
 'The quick ++brown fox',
 'The ""quick"" brown fox',
 'The quick,, brown fox',
 'The ((quick brown fox',
 'The quick brown)) fox',
 'The quick brown fox!!!']

正则表达式呢?这也有助于找到2个或更多连续的标点符号

类似正则表达式的
\([\\-\/\(\)!“+,'&]{2,})\g

{2,}
表示两个或多个


\g
代表全局搜索,第一次匹配时不要停止

标点符号可以放在方括号中的字符类中。 然后,这取决于两个或更多标点符号序列是否由任何标点符号组成,或者标点符号是否相同

在第一种情况下,可以附加大括号来指定最小(2)和最大重复次数。后者是无界的,为空:

[...]{2,} # min. 2 or more
如果只需要找到相同字符的重复,则将第一个匹配的标点符号字符放入一个组中。然后相同组(=相同字符)跟随一个或多个:

([...])\1+
反向引用
\1
表示表达式中的第一个组。由左括号表示的组从左到右编号

下一个问题是转义。Python字符串有转义规则,正则表达式中需要额外的转义。字符类不需要太多转义,但反斜杠必须加倍。因此 下面的示例将反斜杠四倍,一倍是因为字符串,第二倍是因为正则表达式

原始字符串
r'…'
对于模式很有用,但这里需要单引号和双引号

>>> import re
>>> test_cases = [
    "The quick '''brown fox",
    'The &&quick brown fox',
    'The quick\\brown fox',
    'The quick\\\\brown fox',
    'The -quick brown// fox',
    'The quick--brown fox',
    'The (quick brown) fox,,,',
    'The quick ++brown fox',
    'The "quick brown" fox',
    'The quick/brown fox',
    'The quick&brown fox',
    'The ""quick"" brown fox',
    'The quick,, brown fox',
    'The quick brown fox...',
    'The quick-brown fox',
    'The ((quick brown fox',
    'The quick brown)) fox',
    'The quick brown fox!!!',
    "The 'quick' brown fox" ]
>>> pattern_any_punctuation = re.compile('([-/\\\\()!"+,&\'.]{2,})')
>>> pattern_same_punctuation = re.compile('(([-/\\\\()!"+,&\'.])\\2+)')
>>> for t in test_cases:
    match = pattern_same_punctuation.search(t)
    if match:
        print("{:24} => {}".format(t, match.group(1)))
    else:
        print(t)

The quick '''brown fox   => '''
The &&quick brown fox    => &&
The quick\brown fox
The quick\\brown fox     => \\
The -quick brown// fox   => //
The quick--brown fox     => --
The (quick brown) fox,,, => ,,,
The quick ++brown fox    => ++
The "quick brown" fox
The quick/brown fox
The quick&brown fox
The ""quick"" brown fox  => ""
The quick,, brown fox    => ,,
The quick brown fox...   => ...
The quick-brown fox
The ((quick brown fox    => ((
The quick brown)) fox    => ))
The quick brown fox!!!   => !!!
The 'quick' brown fox
>>> 

感谢@Heiko Oberdiek,以下是我正在使用的解决问题的确切代码:(我在标点符号列表中添加了.)

这准确地涵盖了我的所有测试用例:

The quick '''brown fox
The &&quick brown fox
The quick\brown fox
The quick\\brown fox
The -quick brown// fox
The quick--brown fox
The (quick brown) fox,,,
The quick ++brown fox
The "quick brown" fox
The quick/brown fox
The quick&brown fox
The ""quick"" brown fox
The quick,, brown fox
The quick brown fox…
The quick-brown fox
The ((quick brown fox
The quick brown)) fox
The quick brown fox!!!
The 'quick' brown fox
 1 The quick '''brown fox   => '''
 2 The &&quick brown fox    => &&
 3 The quick\\brown fox     => \\
 4 The -quick brown// fox   => //
 5 The quick--brown fox     => --
 6 The (quick brown) fox,,, => ,,,
 7 The quick ++brown fox    => ++
 8 The ""quick"" brown fox  => ""
 9 The quick,, brown fox    => ,,
10 The quick brown fox...   => ...
11 The ((quick brown fox    => ((
12 The quick brown)) fox    => ))
13 The quick brown fox!!!   => !!!

我能看到的唯一问题是,它没有找到双反斜杠\\。我回答时,反斜杠不在字符列表中。(至少它没有出现在呈现的问题中。它可能在源代码中,但没有出现,因为它是转义字符。)您可以添加它。是的,我想使用正则表达式(regex).问题是这样做的语法是什么..你可以在一个在线的正则表达式测试仪上玩。我不知道如何使用你上面的东西,因为没有提到测试用例。你实际上如何使用你上面粘贴的东西?重新编译,然后重新搜索?谢谢。唯一让我困惑的是反斜杠加倍了。我的输入将有两个反斜杠,我希望将其捕获为双反斜杠,但我的测试用例需要四个反斜杠才能检测到双反斜杠。如何确保输入流中看起来是双反斜杠的内容被捕获为双反斜杠而不是单反斜杠?在不带前缀的Python字符串中
r
反斜杠加倍。@ChrisNielsen当用作普通字符时,反斜杠需要在正则表达式中加倍。因此,在Python字符串中的正则表达式中需要四个反斜杠(不带前缀
r
)要得到一个反斜杠。啊,现在更有意义了。谢谢!在您的解释中,您使用了
\1
,但在代码中,您使用了
\\2
。这是为什么?@ChrisNielsen第一个反斜杠使用
“…”
来转义python字符串文本中的第二个反斜杠。使用
,可以避免第一个反斜杠
r'…'
。我没有使用后者,因为它不会使代码更可读,因为两个字符串引号字符都是正则表达式的一部分。