用于匹配重复的连续标点符号的正则表达式,3个句点除外

n0m*_*rcy 7 .net c# regex punctuation

我有正则表达式

(\p{P})\1 
Run Code Online (Sandbox Code Playgroud)

它成功匹配重复的连续标点字符,如

;;
,,
\\
Run Code Online (Sandbox Code Playgroud)

,但我需要排除3个句点(省略号)标点符号.

...
Run Code Online (Sandbox Code Playgroud)

aca*_*lon 3

请小心,因为某些方法无法成功匹配以下形式的字符串.##(即重复标点符号之前的“.”)。假设这是应该匹配的东西。

该解决方案满足以下要求:-

  1. 匹配重复的标点符号。
  2. 省略号 (...) 不匹配。
  3. 两个点 (..) 和四个或更多点相匹配。
  4. 当前面或后面有点时,重复的标点符号会被匹配,例如.##

这是正则表达式:

(?>(\p{P})\1+)(?<!([^.]|^)\.{3})
Run Code Online (Sandbox Code Playgroud)

解释:

  • ?>意思是原子分组。具体来说,扔掉所有回溯位置。这意味着如果“...”无法匹配,则不要后退并尝试匹配“...”。
  • (\p{P})\1+)意味着匹配 2 个或更多标点符号 - 你已经有了这个。
  • (?<!([^.]|^)\.{3})表示从重复字符匹配的末尾向后搜索,如果发现前面没有点或字符串开头的三个点,则失败。这使三个点失效,同时允许两个点或四个点或更多点起作用。

以下测试用例通过并说明了使用:

string pattern = @"(?>(\p{P})\1+)(?<!([^.]|^)\.{3})";

//Your examples:
Assert.IsTrue( Regex.IsMatch( @";;", pattern ) );
Assert.IsTrue( Regex.IsMatch( @",,", pattern ) );
Assert.IsTrue( Regex.IsMatch( @"\\", pattern ) );
//two and four dots should match
Assert.IsTrue( Regex.IsMatch( @"..", pattern ) );
Assert.IsTrue( Regex.IsMatch( @"....", pattern ) );

//Some success variations
Assert.IsTrue( Regex.IsMatch( @".;;", pattern ) );
Assert.IsTrue( Regex.IsMatch( @";;.", pattern ) );
Assert.IsTrue( Regex.IsMatch( @";;///", pattern ) );            
Assert.IsTrue( Regex.IsMatch( @";;;...//", pattern ) ); //If you use Regex.Matches the matches contains ;;; and // but not ...
Assert.IsTrue( Regex.IsMatch( @"...;;;//", pattern ) ); //If you use Regex.Matches the matches contains ;;; and // but not ...            

//Three dots should not match
Assert.IsFalse( Regex.IsMatch( @"...", pattern ) );
Assert.IsFalse( Regex.IsMatch( @"a...", pattern ) );
Assert.IsFalse( Regex.IsMatch( @";...;", pattern ) );                        

//Other tests
Assert.IsFalse( Regex.IsMatch( @".", pattern ) );
Assert.IsFalse( Regex.IsMatch( @";,;,;,;,", pattern ) );  //single punctuation does not match                        
Assert.IsTrue( Regex.IsMatch( @".;;.", pattern ) );
Assert.IsTrue( Regex.IsMatch( @"......", pattern ) );                                       
Assert.IsTrue( Regex.IsMatch( @"a....a", pattern ) );
Assert.IsFalse( Regex.IsMatch( @"abcde", pattern ) );     
Run Code Online (Sandbox Code Playgroud)