Search Within a String That Does Not Contain a Pattern

Match string does not contain substring with regex

Just use a lookahead to check anything following the URL must be a space or line end.

\bhttp:\/\/www\.website\.com\/(?:(?!b=[0-9]+).)*?\b(?= |$)

DEMO

Regular expression to match a line that doesn't contain a word

The notion that regex doesn't support inverse matching is not entirely true. You can mimic this behavior by using negative look-arounds:

^((?!hede).)*$

Non-capturing variant:

^(?:(?!:hede).)*$

The regex above will match any string, or line without a line break, not containing the (sub)string 'hede'. As mentioned, this is not something regex is "good" at (or should do), but still, it is possible.

And if you need to match line break chars as well, use the DOT-ALL modifier (the trailing s in the following pattern):

/^((?!hede).)*$/s

or use it inline:

/(?s)^((?!hede).)*$/

(where the /.../ are the regex delimiters, i.e., not part of the pattern)

If the DOT-ALL modifier is not available, you can mimic the same behavior with the character class [\s\S]:

/^((?!hede)[\s\S])*$/

Explanation

A string is just a list of n characters. Before, and after each character, there's an empty string. So a list of n characters will have n+1 empty strings. Consider the string "ABhedeCD":

    ┌──┬───┬──┬───┬──┬───┬──┬───┬──┬───┬──┬───┬──┬───┬──┬───┬──┐
S = │e1│ A │e2│ B │e3│ h │e4│ e │e5│ d │e6│ e │e7│ C │e8│ D │e9│
└──┴───┴──┴───┴──┴───┴──┴───┴──┴───┴──┴───┴──┴───┴──┴───┴──┘

index 0 1 2 3 4 5 6 7

where the e's are the empty strings. The regex (?!hede). looks ahead to see if there's no substring "hede" to be seen, and if that is the case (so something else is seen), then the . (dot) will match any character except a line break. Look-arounds are also called zero-width-assertions because they don't consume any characters. They only assert/validate something.

So, in my example, every empty string is first validated to see if there's no "hede" up ahead, before a character is consumed by the . (dot). The regex (?!hede). will do that only once, so it is wrapped in a group, and repeated zero or more times: ((?!hede).)*. Finally, the start- and end-of-input are anchored to make sure the entire input is consumed: ^((?!hede).)*$

As you can see, the input "ABhedeCD" will fail because on e3, the regex (?!hede) fails (there is "hede" up ahead!).

Search within a string that does not contain a pattern

grepl returns a logical vector. You can use the ! operator if you want the opposite result.

data$ID[!grepl("xyx", data$ID) & data$age>60]

Regex: How to find substring that does NOT contain a certain word

Using a tempered dot, we can try:

string = "STARTcandyFINISH  STARTsugarFINISH STARTpoisonFINISH STARTBlobpoisonFINISH STARTpoisonBlobFINISH"
matches = re.findall(r'START((?:(?!poison).)*?)FINISH', string)
print(matches)

This prints:

['candy', 'sugar']

For an explanation of how the regex pattern works, we can have a closer look at:

(?:(?!poison).)*?

This uses a tempered dot trick. It will match, one character at a time, so long as what follows is not poison.

Regular expression to match strings that do NOT contain all specified elements

Nice question. It looks like you are looking for some AND logic. I am sure someone can come up with something better, but I thought of two ways:

^(?=(?!.*\btwo\b)|(?!.*\bthree\b)).*$

See the online demo

Or:

^(?=.*\btwo\b)(?=.*\bthree\b)(*SKIP)(*F)|^.*$

See the online demo

In both cases we are using positive lookahead to mimic the AND logic to prevent both words being present in a text irrespective of their position in the full string. If just one of those words is present, the string will pass.

How to match a text in a string, that does not contain a specific word and does not contain a word with letters and digits?

Perhaps this will match your values using a word boundary and a negative lookahead:

\b(?!\w*abc)[^\W\d]+\b
  • \b Word boundary
  • (?!\w*abc) Assert what is on the right does not contain abc
  • [^\W\d]+ Negated character class, match 1+ times a word character except a digit
  • \b Word boundary

Regex demo

Regular Expression for searching a string starting with string 1, does not containing string 2, and ends in string 3

Look:

  • starts with string 1 - string1
  • doesn't contain string2 in between strings 1 and 3 - here, you need to use a . tempered with a negative lookahead - (?:(?!string1|string2).)*? (note that to match across line, in Visual Studio S&R you need to use [\s\S\r] instead of a . (\r is necessary here for the reason [\s\S] does not match a line break in VS S&R regex))
  • ends with string 3 - string3.

So, the whole expression is

string1(?:(?!string1|string2).)*?string3

See the regex demo.

Regular Expression - Match pattern that does not contain a string

Your syntax for trying to negate part of your pattern is incorrect.

Also, ^ outside of a character class asserts position at the beginning of the string. You need to use a Negative Lookahead assertion and be sure to anchor the entire pattern.

^abc(?:(?!abc).)*abc$

Live Demo

How do I find files that do not contain a given string pattern?

The following command gives me all the files that do not contain the pattern foo:

find .  -not  -ipath '.*svn*' -exec  grep  -H -E -o -c  "foo"  {} \; | grep 0


Related Topics



Leave a reply



Submit