Search for n characters with exactly (or at least one)
I am having trouble generating regex. I'm not even sure if this is possible at all.
I want to match n characters, but one of them must be a line break (or any specific character).
This is my input:
0000000 0000000 000A000 00AB000 AAAB000 ABBB000
My (not working) regex is
.*A.{5}A.{5}A.{5}A.*
Changing the mode to is DOTALL
not enough, as I have to make sure that between each matchedA
I just want to know if my input matches, I don't want to fetch anything.
I want to check if my input has a A
-diagonal.
source to share
Self-referring groups (Qtax Trick)
/^(?:.(?=.*+\n(\1?+.).*+\n(\2?+.).*+\n(\3?+.)))*?...A.*+\n\1?+..A.*+\n\2?+.A.*+\n\3?+A/m
Explanation:
^
The beginning of the line.(?:.
Matches any character other than a newline.-
(?=
Positive "look ahead": states that the following can be compared. This part is for the capture. -
.*+\n
Matches everything up to a line and then on a new line. -
(\1?+.)
-
?+
: if this group matches, consume and add the symbol to it, otherwise just match the symbol and work your way forward.
-
-
.*+\n
Matches everything up to the next line, just like above. -
(\2?+.)
Same as subpattern 1. -
.*+\n
promotes the line. -
(\3?+.)
Same as subpatterns 1 and 2. -
)
Completes "foresight". )*?
Zero or more, reluctant to match .
The above group does the following. Pay attention to the colored groups:
(source: gyazo.com )
However, because this group is reluctant to quantify, this happens:
(source: gyazo.com )
Note that although colored groups may or may not be captured, the location of the pointer remains the same during capture. Therefore, at the very first iteration, all capture groups do not capture anything. This brings us to the next part of the regular expression:
...A
Three characters, then "A" (literal character).-
.*+\n
Skip the rest of the line and the newline character ... \1?+
If we captured the first group, consume it!-
..A
Two characters, then "A" (literal character). -
.*+\n
Next line. \2?+
Consume whenever possible.-
.A
You understand, but I will write the text anyway. The same as above. -
.*+\n
Advance. \3?+
......A
End of the match!
If you don't like the text, I'll just draw it again:
(source: gyazo.com )
May everyone bow to our master -
'vertical' regex matching in 'image' ASCII
Here is a demo of the code , and here is a demo of regular expressions for the electronic x
version .
source to share
With DOTALL it .
also counts in new characters. Perhaps you want, for example, A(?:[\r\n]*[^\r\n]){5}
where I used [\r\n]
as a newline and negation [^\r\n]
as a non-newline.
This is repeated at least 4 times with any number of characters before or after it:
.*?(?:A(?:[\r\n]*[^\r\n]){5}[\r\n]*){4,}.*
Additionally, you can use a negative lookahead after A
to check if there is a sequence of at least 7 characters without the string:, (?![^\r\n]{7})
so the template will look like this:
.*?(?:A(?![^\r\n]{7})(?:[\r\n]*[^\r\n]){5}[\r\n]*){4,}.*
source to share