Hello @dimitrov, @alan-kilborn, @peterjones, @astrosofista and All,
Two days later, I realize that my generic regex could be shortened a bit ! Reading, again, what I said in my previous post :
Part WITHOUT Hi (Part WITHOUT Hi + Hi) x 4 Part WITHOUT Hi
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ¯¯ ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
| | | |
V V V V
(?x-s) ^ ( (?! Hi ).)* ( ((?!Hi).)*? Hi ){4} ((?!Hi).)* $ # The INITIAL regex
You may see, as I did, that the regex begins with “Part WITHOUT Hi” and ( “Part WITHOUT Hi” and “Hi” ) repeated four times. So, there is a redundant part in this expression !
The initial regex should be, simply :
^ (Part WITHOUT Hi + Hi) x 4 + Part WITHOUT Hi $
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ¯¯ ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
| | |
V V V
(?x-s) ^ ( ((?!Hi).)*? Hi ){4} ((?!Hi).)* $ # The INITIAL regex
Thus, the four generic regexes to solve general cases are simplified :
To match lines containing your string,
whatever the case, as a
whole expression, the
number of times,
within the range :
(?i-s)^(?:((?:(?!(\bYour string\b)).)*?)(?2)){Your range}(?1)$
To match lines containing your string,
whatever the case, the
number of times,
within the range :
(?i-s)^(?:((?:(?!(Your string)).)*?)(?2)){Your range}(?1)$
To match lines containing your string, with its
exact case, as a
whole expression, the
number of times,
within the range :
(?-is)^(?:((?:(?!(\bYour string\b)).)*?)(?2)){Your range}(?1)$
To match lines containing your string, with its
exact case, the
number of times,
within the range :
(?-is)^(?:((?:(?!(Your string)).)*?)(?2)){Your range}(?1)$
For instance, all the regexes, below, are valid :
(?i-s)^(?:((?:(?!(\bHi\b)).)*?)(?2)){4}(?1)$
(?i-s)^(?:((?:(?!(Hi)).)*?)(?2)){3,5}(?1)$
(?i-s)^(?:((?:(?!(\bThis is a test\b)).)*?)(?2)){2,4}(?1)$
(?-is)^(?:((?:(?!(\bThis is a test\b)).)*?)(?2)){2,4}(?1)$
Just test them against this text below, pasted in a new tab : :
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---Hi-----
---Hi----Hi----
---Hi----Hi-----Hi----
---Hi----Hi-----Hi----Hi----
---Hi----Hi-----Hi----Hi----Hi----
---Hi----Hi-----Hi----Hi----Hi----Hi-----
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---This is a test-----
---This is a test----This is a test----
---This is a test----This is a test-----This is a test----
---This is a test----This is a test-----This is a test----This is a test----
---This is a TEST----This is a TEST-----This is a TEST----This is a TEST----
---This is a test----This is a test-----This is a test----This is a test----This is a test----
---This is a test----This is a test-----This is a test----This is a test----This is a test----This is a test-----
Below, a list of the different steps in the genesis of the generic regex which should help you to understand this tricky regex !
^ ( Part WITHOUT Hi + Hi) x 4 + Part WITHOUT Hi $
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ¯¯ ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
| | |
V V V
(?x-s) ^ ( ( (?! Hi ).)*? Hi ){4} ( (?!Hi).)* $ # The INITIAL regex
(?x-s) ^ (?: (?: (?! Hi ).)*? Hi ){4} (?: (?!Hi).)*? $ # We change all GROUPS as NON-CAPTURING, add a LAZY quantifier and notice 2 IDENTICAL blocks !
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Gr1
V
(?x-s) ^ (?: ( (?: (?! Hi ).)*? ) Hi ){4} (?: (?!Hi).)*? $ # We create a NEW group 1 AROUND the FIRST block
–– ––
Gr1
V
(?x-s) ^ (?: ( (?: (?! Hi ).)*? ) Hi ){4} (?1) $ # We replace the 2nd BLOCK by a SUB-ROUTINE CALL to GROUP 1 (?1)
¯¯ ¯¯
Gr1 Gr2
V V
(?x-s) ^ (?: ( (?: (?! (Hi) ).)*? ) Hi ){4} (?1) $ # We create a NEW group 2 AROUND the FIRST string "Hi"
Gr1 Gr2
V V
(?x-s) ^ (?: ( (?: (?! (Hi) ).)*? ) (?2) ){4} (?1) $ # We replace THE 2nd string "Hi" by a SUB-ROUTINE CALL to GROUP 2 (?2)
(?-s)^(?:((?:(?!(Hi)).)*?)(?2)){4}(?1)$ # We suppress the FREE-SPACING mode and DELETE any SPACE character
(?i-s)^(?:((?:(?!(Hi)).)*?)(?2)){4}(?1)$ # We add the CASE modifier
(?i-s)^(?:((?:(?!(\bHi\b)).)*?)(?2)){4}(?1)$ # We add the \b BOUNDARIES to get a WHOLE expression
^
|
For the 3 LAST regexes, STOP
the SELECTION at the $ sign
You may test any of these regexes above, against the text below, pasted in a new tab. It matches only the line containing exactly four strings Hi !
---Hi-----
---Hi----Hi----
---Hi----Hi-----Hi----
---Hi----Hi-----Hi----Hi----
---Hi----Hi-----Hi----Hi----Hi----
---Hi----Hi-----Hi----Hi----Hi----Hi-----
Best regards,
guy038