I know that in Regex, you can reject lists of symbols, such as [^abc]
. I'd like to reject upon seeing an entire word in the middle of my input.
To be more precise, I'd like to reject "print <Anything except "all">". A few examples:
print all - match
frokenfooster - no match
print all nomnom - no match
print bollocks - no match
print allpies - no match
You're looking for a negative look-ahead. (ref. using look-ahead and look-behind)
(?!exclude)
Would disqualify the word "exclude" in the pattern.
Regular expressions support a word-break \b
.
Searching for the existence of the word "all" in a string is as simple as:
>> 'the word "all"'[/\ball\b/] #=> "all"
>> 'the word "ball"'[/\ball\b/] #=> nil
>> 'all of the words'[/\ball\b/] #=> "all"
>> 'we had a ball'[/\ball\b/] #=> nil
>> 'not ball but all'[/\ball\b/] #=> "all"
Note, it didn't take anchoring it to the start or end of a string, because \b
recognizes the start and end of the string as word boundaries also.
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