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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.