i searching to find some Perl Regular Expression Syntax about some requirements i have in a project. First i want to exclude strings from a txt file (dictionary).
For ex
To not match a word from a file you might check whether a string contains a substring or use a negative lookahead and an alternation:
^(?!.*(?:tree|car|ship)).*$
^
Assert start of string(?!
negative lookahead, assert what is on the right is not
.*(?:tree|car|ship)
Match 0+ times any char except a newline and match either tree car or ship)
Close negative lookahead .*
Match any char except a newline$
Assert end of stringRegex demo
To not allow a string to have over 3 times a char repeat you could use:
\b(?!(?:\w*(\w)\1){3})\w+\b
\b
Word boundary(?!
Negative lookahead, assert what is on the right is not
(?:
NOn capturing group\w*(\w)\1
Match 0+ times a word character followed by capturing a word char in a group followed by a backreference using \1
to that group){3}
Close non capturing group and repeat 3 times)
close negative lookahead\w+
Match 1+ word characters\b
word boundaryRegex demo
Update
According to this posted answer (which you might add to the question instead) you have 2 patterns that you want to combine but it does not work:
(?=^(?!(?:\w*(.)\1){3}).+$)(?=^(?:(.)(?!(?:.*?\1){4}))*$)
In those 2 patterns you use 2 capturing groups, so the second pattern has to point to the second capturing group \2
.
(?=^(?!(?:\w*(.)\1){3}).+$)(?=^(?:(.)(?!(?:.*?\2){4}))*$)
^
Pattern demo