Regex to select filenames which is not preceded by -map before .scss.
something-something.scss -> Match
something-map.scss -> Don't match
something.scss -> Match
Tried
[a-zA-Z]+(?!-map).scss
but not working. It select all files.
I would suggest to use the negative lookbehind:
.*(?<!map)\.scss
The explanation: It tells the regex engine to temporarily step backwards in the string, to check if the text inside the lookbehind can be matched there. (?<!a)b matches a “b” that is not preceded by an “a”, using negative lookbehind. It doesn’t match cab, but matches the b (and only the b) in bed or debt. (?<=a)b (positive lookbehind) matches the b (and only the b) in cab, but does not match bed or debt.
You need a negative-lookbehind. Change your regex to:
(?<!-map)\.scss
This matches all .scss files that are not preceded by "-map".
Related
Think about two different highlight match in vim
Pattern 1.
syn match match1 /\$[^$ ]+\$/
Match $foo$, $bar$, $123$
Pattern 2.
syn match match2 /(\w+\|(\$[^\$]+\$)\#=)+__/
I want it match foo$bar$__ but not $bar$
The problem is Pattern1 will conflict with Pattern2.
I'm trying to use Positive Lookahead to bypass Pattern1 in Pattern2,
but the prefix __ (Double underscores) destroy the behavior of Positive lookahead.
How do I solve this issue? or i'm doing something wrong !?
Update:
Sorry for bad explanation.
Pattern 1 match any string surrounded by two dollar signs
syn match match1 /\$[^$ ]\+\$/
-> $foo$, $bar$
Pattern 2 match any string end with double underscores BUT match still but exclude any string that match as Pattern1.
syn match match2 /\(\w\+\|\(\$[^\$]\+\$\)\#=\)\+__/
-> hello__, world__
so the problem is when I add any string related to pattern 1
hello$foo$__
in this case. I want hello AND __ match with pattern 1(Continuous)
but also let $foo$ match with pattern 2.
I don't think you understand what lookahead does. It looks like what you're trying to do is to match a string, but skip over parts of it:
foo$bar$___
^^^~~~~~^^^
... where the parts marked ^ form the match proper (discontinous) and the parts marked ~ are skipped over.
This is not possible with a regex. A regex always matches a continuous piece of string.
What lookahead does is it lets you "peek ahead": It matches a sub-regex as usual, but does not move the current position within the string. Depending on where you put the lookahead, this lets you either check text beyond the end of the match or make sure the same string is matched by two regexes simultaneously (although the latter can also be done with \& in vim).
Example:
\%(foo\)\#=bar
This can never match. It requires the next three characters to be both foo and bar at the same time, which is impossible.
I think what you're looking for is overlapping matches. Vim supports this directly:
syn match match1 /\$[^$ ]\+\$/
syn match match2 /\%(\w\|\$[^$ ]\+\$\)\+__/ contains=match1
Here we're saying matches of match2 can contain matches of match1. This gives you the highlighting you want.
I am looking for a regex which does the following:
//SPECIAL_WORD some text -> Should match
//SPECIAL_WORD (123456) -> Should match
//SPECIAL_WORD 123456 -> Should NOT match
=> Basically anything other then 'SPECIAL_WORD blank 6 digits' should match if the SPECIAL_WORD is found.
I found how I can match the positive case SPECIAL_WORD\s\d{6}
I tried the positive lookahead however didn't get it to work: (?!SPECIAL_WORD\s\d{6}). I also tried to negate the whole thing \b(?=\w)(?!SPECIAL_WORD\s\d{6})\b(\w*)however then everything else is matched...
Any ideas?
You should match SPECIAL_WORD then go for a negative lookahead:
\bSPECIAL_WORD\s(?!\d{6}\b)
\b assures that both ends are not part of a word. You may not need them.
Live demo
How can I match strings that aren't preceded by an # sign?
/(?!#)(somestring|someotherstring)/
Doesn't produce expected results. Am testing this in sublime text following Sublime: Regular Expressions.cheatsheet
You need to use a lookbehind:
(?<!#)(somestring|someotherstring)
The (?!#) lookahead will check if the following symbol is not a #.
Some more details:
Lookbehind has the same effect [VS: as lookahead], but works backwards. It tells the regex engine to temporarily step backwards in the string, to check if the text inside the lookbehind can be matched there. (?<!a)b matches a b that is not preceded by an a, using negative lookbehind. It doesn't match cab, but matches the b (and only the b) in bed or debt.
Negative lookbehind is written as (?<!text), using an exclamation point instead of an equals sign.
I have string like this:
12abcc?p_auth=123ABC&ABC&s
Start of symbol is "p_auth=" and end of string first "&" symbol.
P.S symbol '&' and 'p_auth=' must not be included.
I have wrote that regex:
(p_auth).+?(?=&)
Ok, thats works well, it gets that sub-string:
p_auth=123ABC
bot how to get string without 'p_auth'?
Use look-arounds:
(?<=p_auth=).*?(?=&)
See regex demo
The look-behind (?<=p_auth=) and the look-ahead (?=&) do not consume characters as they are zero-width assertions. They just check for the substring presence either before or after a certain subpattern.
A couple more words about (?<=p_auth=). It is a positive look-behind. Positive because it require a pattern inside it to appear on the left, before the "main" subpattern. If the look-behind subpattern is found, the result is just "true" and the regex goes on checking the rest of subpatterns. If not, the match is failed, the engine goes on looking for another match at the next index.
Here is some description from regular-expressions.info:
It [the look-behind] tells the regex engine to temporarily step backwards in the string, to check if the text inside the lookbehind can be matched there. (?<!a)b matches a "b" that is not preceded by an "a", using negative lookbehind. It doesn't match cab, but matches the b (and only the b) in bed or debt. (?<=a)b (positive lookbehind) matches the b (and only the b) in cab, but does not match bed or debt.
In most cases, you do not really need look-arounds. In this case, you could just use a
p_auth(.*?)&
And get the first capturing group value.
The .*? pattern will look for any number of characters other than a newline, but as few as possible that are required to find a match. It is called lazy dot matching, because the ? symbol makes the * quantifier stop before the first symbol that is matched by the subsequent subpattern in the regular expression.
The .*& would match all the substring until the last & because * quantifier is greedy - it will consume as many characters it can match as possible.
See more at Repetition with Star and Plus regular-expressions.info page.
p_auth(.+?)(?=&)
Simply use this and grab the group 1 or capture 1.
I need a RegEx that matches when the string is not 1234, not 6789, and not blank.
1234 -> not a match
6789 -> not a match
[blank] -> not a match
abc -> match
5431 -> match
The RegEx engine is the one bundled in the JDK 6, if that matters.
Thanks
Try using negative look aheads:
^(?!.*1234.*$)(?!.*5677.*$)(?!=\s*$).+
This negative lookahead should work:
^(?!.*?\b(1234|5677)\b).+$
Word boundaries \b is to make sure that you don't disallow 11234 and 56777 etc.
.+ will make sure to not to match blank input.