I haven't used regex much and I'm trying to learn a bit of it now.
I want to look for three possible patterns in a string at least two times. the patterns I'm looking for are at the start of a string, it can be 2;
, somewhere in the middle of a string, it can be ;2;
and at the end of a string, it can be ;2
The what I'm doing now is (^2;.*|.*;2;.*|.*;2$){2}
.
This seems to work for the most part, but I cant figure out why a2;2;2
isn't matching. the ;2;
, and ;2
should match I believe.
CodePudding user response:
Quantifiers won't count overlapping occurrences of the pattern. Since the ;
characters are included in the match, you won't find matches where the ending ;
of one match is the starting ;
of the other.
You can solve this by using lookarounds to take the ;
out of the match.
(?<=^|;)2(?=;).*(?<=;)2(?=;|$)
CodePudding user response:
You could try (with look-arounds around the 2
):
(?:^2(?=;)|(?<=;)2(?=;)).*(?<=;)2(?=;|$)