Regular expression to match repeated consecutive punctuation except three periods

I have a regex

(\p{P})\1 

which matches duplicate consecutive punctuation characters such as

 ;; ,, \\ 

but I need to exclude punctuation from 3 periods (ellipsis).

 ... 
+7
c # regex punctuation
source share
3 answers

Be careful, as some approaches will not successfully match the lines of the form .## (ie "." Before repeating punctuation). Assuming this is what should match.

This solution meets the following requirements: -

  • Punctuation is repeated.
  • Ellipsis (...) does not match.
  • Two points (..) and four or more points are mapped.
  • Repeating punctuation is consistent when preceded or followed by periods, for example. .##

This is a regex:

 (?>(\p{P})\1+)(?<!([^.]|^)\.{3}) 

Explanation:

  • ?> means atomic grouping . In particular, discard all return items. This means that if "..." does not match, then do not back down and do not try to match "..".
  • (\p{P})\1+) means matching two or more punctuation characters - you already had that.
  • (?<!([^.]|^)\.{3}) means looking back from the end of the repeated match of characters and failing if you find three points that are not preceded by a point or the beginning of a line. This fails three points, allowing you to work with two points or four points or more.

The following test cases pass and illustrate use:

 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 ) ); 
+3
source share

To avoid matching ...

 (?<![.])(?![.]{3})(\p{P})\1 
+2
source share
 (?<!\.)(?!\.{3}(?!\.))(\p{P})\1+ 

This will match any repeated punctuation (including .... or ...... etc.), unless it is a string ... For example:

 ; -- No Match ;; -- Match ,, -- Match ,,,, -- Match \\ -- Match ... -- No Match .... -- Match 
+2
source share

All Articles