For example, I want to exclude 'fitting', 'hollow', 'trillion'
but not 'hello' or 'pattern'
I already got the following to work
(.)(.)\2\1
which matches 'hollow' or 'fitting', but I have trouble negating this.
the closest thing I get is
^.(?!(.)(.)\2\1)
which excludes 'fitting' and 'hollow' but not 'trillion'
To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself.
The function str_trim() trims the whitespace from a string. The opposite of str_trim() is str_pad() which adds characters to each side.
The idea is very simple. For every pair (x, y) of consecutive characters in the pattern string, we find the last occurrence of x and first occurrence of y in the input string. If last occurrence of character x is after first occurrence of character y for any pair, we return false.
Perform Case-Insensitive Matches By default, regexp performs case-sensitive matching. The regular expression specifies that the character vector: Begins with any number of alphanumeric or underscore characters, \w* . Ends with the literal text case .
It's a little different from what you have. Your current regex will check for the pallindromicity (?) as of the second character. Since you want to check the whole string, you need to change it a little to:
^(?!.*(.)(.)\2\1)
The first anchor will ensure that the check is made only at the beginning (otherwise, the regex can claim a match at the end of the string).
Then the .*
within the negative lookahead will enable the check to be done anywhere within the string. If there's any match, fail the entire match.
It doesn't match with trillion because you added ^.
means it must have a character before the match from beginning. For your first two cases it has h
and f
character. So if you change this into ^..(?!(.)(.)\2\1)
then it will work for trillion
.
So in general the regex will be:
(?!.*(.)(.)\2\1)
^^ any number of characters(other than \n)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With