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