terewcoin.blogg.se

Regular expression not containing substring
Regular expression not containing substring






regular expression not containing substring

However if 'hede' does not present, both would be equal slow.įor more information about lookahead, please check out the great article: Mastering Lookahead and Lookbehind.Īlso, please check out RegexGen.js, a JavaScript Regular Expression Generator that helps to construct complex regular expressions. Note the (*?) lazy quantifier in the negative lookahead part is optional, you can use (*) greedy quantifier instead, depending on your data: if 'hede' does present and in the beginning half of the text, the lazy quantifier can be faster otherwise, the greedy quantifier be faster. Here is the improved regex: /^(?!.*?hede).*$/ We can avoid this and let the lookahead part check out the whole text, ensure there is no 'hede', and then the normal part (.*) can eat the whole text all at one time. However, with Bart Kiers' answer, the lookahead part will test 1 to 4 characters ahead while matching any single character. This is answered and explained by Bart Kiers. With negative lookahead, regular expression can match something not contains specific pattern. You can see this example here, and try Vcsn online there.

regular expression not containing substring

Where + is usually denoted |, \e denotes the empty word, and is usually written. Vcsn supports this operator (which it denotes ') eĬonvert this expression to an automaton: In : a = e.automaton() aįinally, convert this automaton back to a simple expression. Finally, the start- and end-of-input are anchored to make sure the entire input is consumed: ^((?!hede).)*$Īs you can see, the input "ABhedeCD" will fail because on e3, the regex (?!hede) fails (there is "hede" up ahead!).įWIW, since regular languages (aka rational languages) are closed under complementation, it's always possible to find a regular expression (aka rational expression) that negates another expression. will do that only once, so it is wrapped in a group, and repeated zero or more times: ((?!hede).)*. 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.

regular expression not containing substring

Look-arounds are also called zero-width-assertions because they don't consume any characters. (dot) will match any character except a line break. 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. So a list of n characters will have n+1 empty strings. Before, and after each character, there's an empty string. If the DOT-ALL modifier is not available, you can mimic the same behavior with the character class : /^((?!hede))*$/Ī string is just a list of n characters. (where the /./ are the regex delimiters, i.e., not part of the pattern) As mentioned, this is not something regex is "good" at (or should do), but still, it is possible.Īnd if you need to match line break chars as well, use the DOT-ALL modifier (the trailing s in the following pattern): /^((?!hede).)*$/s The regex above will match any string, or line without a line break, not containing the (sub)string 'hede'. You can mimic this behavior by using negative look-arounds: ^((?!hede).)*$ The notion that regex doesn't support inverse matching is not entirely true.








Regular expression not containing substring