Reputation: 123
I'm having trouble when trying to find stuff with regex.
I have the following regex
([{\-\+~a-zA-Z0-9]+){3,}
And it works as it should, however, I need to capture all possible matches AFTER a certain character, which in this case is }
.
faz.}a..sd..3·$....................foo....e..P...0...................bar
foo
and bar
must match, but faz
shouldn't. Note that I can't use .+(?=})
because it captures everything after the }
into a single match. I searched in the internet and every answer suggested this pattern.
Thanks in advance.
Upvotes: 1
Views: 397
Reputation:
Get it all at once with a simplified and quicker CaptureCollection
(?s)}(?:.*?([{\-+~a-zA-Z0-9]{3,}))+
Read version
(?s)
}
(?:
.*?
( [{\-+~a-zA-Z0-9]{3,} ) # (1)
)+
C#
string inp = @"faz.}a..sd..3·$....................foo....e..P...0...................bar";
Regex Rx1 = new Regex( @"(?s)}(?:.*?([{\-+~a-zA-Z0-9]{3,}))+" );
Match M1 = Rx1.Match( inp );
if ( M1.Success )
{
CaptureCollection cc = M1.Groups[1].Captures;
for (int i = 0; i < cc.Count; i++)
Console.WriteLine("{0}", cc[i].Value);
}
Output
foo
bar
Press any key to continue . . .
Upvotes: 0
Reputation: 626748
In .NET, you may use a positive variable length lookbehind to match the pattern occurrences only after a certain char/substring/pattern:
(?<=}.*)[-{+~a-zA-Z0-9]{3,}
^^^^^^^^
See the regex demo
Details
(?<=}.*)
- a }
(and any 0+ chars other than a newline) should be present before the subsequent cosuming pattern part[-{+~a-zA-Z0-9]{3,}
- 3 or more occurrences of letters, digits and some punctuation/symbol chars listed in the character class.You might also check an opposite approach: match the words you need if they are not followed with }
:
[-{+~a-zA-Z0-9]{3,}(?![^}]*})
See this regex demo.
Here, (?![^}]*})
is a negative lookahead that fails the match if there is a }
after any 0+ chars other than }
immediately to the right of the "word" matched with the [-{+~a-zA-Z0-9]{3,}
pattern.
Upvotes: 1