RegEx matching String not matching patterns and not blank - regex

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.

Related

Regex: Exclude word with word `-map` before `.scss`

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".

Regex to match violations if a special word is found

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

Regex extract string between 2 strings, that contains 3rd string

I have this regex
(?<=TG00).*?(?=#)
which extracts all strings between TG00 and #. Demo: https://regex101.com/r/04oqua/1
Now, from above results I want to extract only the string which contains TG40 155963. How can I do it?
Try this pattern:
TG00[^#]*TG40 155963[^#]*#
This pattern just says to find the string TG40 155963 in between TG00 and an ending #. For the sample data in your demo there were 3 matches.
Demo
For some reason appending .*? to your lookbehind results in engine error, but works fine with lookahead. Regex below does not match your text exactly, but it does extract it via capture group.
(?<=TG00).*?(TG40 155963)(?=.*?#)
You can use this regex with a lookahead and negated character class:
(?<=TG00)(?=[^#]*TG40 155963)[^#]+(?=#)
RegEx Demo
RegEx Explanation:
(?<=TG00): Assert that we have TG00 at previous position
(?=[^#]*TG40 155963): Lookahead to assert we have string TG40 155963 after 0 or more non-# characters, ahead
[^#]+: Match 1+ non-# characters

Regex - Regular expression for repeat word with prefix

How do I create a regular expression to match subword which start with same prefix, for example aaa, the random word after that has random length.
aaa[randomword1]aaa[randomword2]
If I use pattern
(aaa\w+)*
it match (aaa) and [randomword1]aaa[randomword2]. But I want to match groups: aaa, randomword1, aaa, randomword2.
EDIT: I mean in the string may have multi times aaa, and I need match all subword aaa_randomword_times_n.
I suggest aaa(\w+)aaa(\w+), hope it will help you:)
You can use following regular expression :
\b(aaa|(?<=\[).*?(?=\]))\b
\b..\b -> zero-width assertion word boundary to match word
aaa -> your specific word to look
| -> check for optional
(?<=[) look behind zero width assertion which checks characters after
open square bracket([)
.*? : character to match
(?=])) => look ahead zero width assertion which matches characters
before closing square bracket(])

Match Strings, not preceded by a-z or A-Z, and which contains 1324

Using regex, I want to match strings in a text if:
They contain 1324 as a substring
The 1324 substring is not preceded by one or more characters like a-zA-Z
For instance:
'1324test' should be matched
'1324' should be matched
'test test :/1324test' should be matched
'test test 1324test' should be matched
'test test 1324' should be matched
'test test test1324test' should NOT be matched
Here's my attempt: [^a-zA-Z]+1324
How can I do this ?
Use this RegEx:
(?<![a-zA-Z])1324
Live Demo on Regex101
It uses a Negative Lookbehind (here is a nice site explaining them). This means it will check behind the 1324 (lookbehind it) to see if there are any letters ([a-zA-Z]). If there are, it will fail.
Use negative lookbehind:
.*(?<![a-zA-Z])1324.*
If your regex flavor doesn't support negative look-behinds (e.g. JS) you could do it with
(^|[^a-zA-Z])1324
Match start of string or Non-Alpha Character.
Here is my attempt. I think your second test case should be 1324 not 1234
([^a-zA-Z]+|^)1324
Here is a test link
Update: The plus link is not needed. It just ads overhead. The proper example should be this
([^a-zA-Z]|^)1324
Debuggex Demo
You can match on what you don't want without capturing, then capture what you do want to, then examine the captured portion.
/[a-z]1324|(1324)|.*/i
In JavaScript, your match would be given by
!!input.match(regexp)[1]