-2

I want to use a negative look-ahead inside a non capturing group in Perl Regex. So far i have create an expression that not allows 3 "pairs of characters" into a string

CASE 1: ^(?!(?:\w*(.)\1){3}).+$

For example the string: Mytte!3sttStrring  | Not pass - 3 pairs | "tt","tt","rr"

If you see there is a negative look-ahead before the group, i want to add this expression into a group. To understand this i will present you an other example that disallows only one pair or chars.

CASE 2 : no match if there is a pair in string:   (?=^(?:(.)(?!\1))*$)

If you test these 2 expressions in https://regex101.com/ you will see that for the string "MySampleString" on the CASE 2 I have 2 Match Information. (Full Match and Group 1 Match). Now if you run the first pattern (CASE 1) you will see only the Full Match at Match Information.

In my try add grouping match option in the CASE 1 putting brackets around:

CASE 1 my try:   (^(?!(?:\w*(.)\1){3}).+$)

but now the expression inside doesn't work :/

I want to find a way that ^(?!(?:\w*(.)\1){3}).+$ will work (with Full Match and Group1 Match). I have to add this option to a very long regular expression that I'm working on and it cant work as a full match group. To make it cleaner i will show you an example that the String must not have a "pair of chars" and at the same time not to include 4 repetitions of any letter.

        (?=^(?:(.)(?!\1))*$)(?=^(?:(.)(?!(?:.*?\1){4}))*$)

These 2 expressions (?=^(?:(.)(?!\1))$) and (?=^(?:(.)(?!(?:.?\1){4}))*$) combined because both have a grouping match.

Nikos Kalantas
  • 95
  • 4
  • 11
  • Could you please explain your real problem? What is the regex? I suspect you just want to add a restriction to its beginning, that the string cannot contain 3 or more repeated consecutive chars. You need to make sure the capturing group and the backreferences correspond: `^(?!(?:\w*(.)\1){3}).....`. If you want to check for any chars, replace `\w*` with `.*`. The regex will be very inefficient. – Wiktor Stribiżew Apr 19 '19 at 12:41
  • REGEX: Regular Expression | my real problem is that like i can combine these two different regular Expressions (?=^(?:(.)(?!\1))$) and (?=^(?:(.)(?!(?:.?\1){4}))*$) into one like i show you at the end of the post. I want to make the combination of ^(?!(?:\w*(.)\1){3}).+$ and (?=^(?:(.)(?!(?:.?\1){4}))*$) – Nikos Kalantas Apr 19 '19 at 12:44
  • @WiktorStribiżew Stribiżew Its very clear... Try to run (?=^(?:(.)(?!\1))*$)(?=^(?:(.)(?!(?:.*?\1){4}))*$) that exclude string with (one pair) and not (4 repetitions of a same character ). This one runs... Now try to run (?=^(?!(?:\w*(.)\1){3}).+$)(?=^(?:(.)(?!(?:.*?\1){4}))*$) that i want to exclude string with (thee pairs)and not (4 repetitions of a same character ). This one NOT runs – Nikos Kalantas Apr 19 '19 at 12:47
  • Something [like this](https://regex101.com/r/wtmBQE/1)? `^(?=(?:.*(.)\1){3})(?!(?:.*(.)\2){4})`? You have two *capturing* groups now and the backreference in one is `\1` and in the other it must be `\2`. – Wiktor Stribiżew Apr 19 '19 at 12:55
  • @Wiktor Stribiżew In this link it work if there are 3 pairs of chars (i need no to work if there are no 3 pairs inside my String), also the second expression not worked. For example i want the strings "M@yttesst11" to fail because there are 3 pairs inside, also in the string "@M@yte@st1@" must fail too because even there are not 3 pairs of chars there are 4 repetitions of a char (the second expression).As an example a string that i want to pass is : M@Ytteest --> not over 3 times a pair, not over 4 times a repetition of a char. Thanks for the help by the way – Nikos Kalantas Apr 19 '19 at 13:06
  • Try `^(?!.*(.)(?:.*?\1){3})(?!(?:.*(.)\2){3}).*`, see https://regex101.com/r/wtmBQE/2 – Wiktor Stribiżew Apr 19 '19 at 13:17

1 Answers1

0

if you mean by 'combine' is AND

^(?!(\S)\1)(?!(\S)(?:\S*?\2){4})\S+ 

if you mean by 'combine' is OR

^(?!(\S)\1|(\S)(?:\S*?\2){4})\S+