This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 2 years ago.
I would like to come out with a regex expression that negate the matched results of regex expression: .google.*search. And, is it possible to achieve it with regex from the regex expression I am trying to negate?
Test data
[1] https://www.google.com/search?newwindow=1&sxsrf=ALeKk02MzEfbUp3jO4Np
[2] https://github.com/redis/redis-rb
[3] https://web.whatsapp.com/
Expected result
Row 2, 3 match the regex pattern and are part of the results.
the following regex does the trick
^(?!.+google.*search)
basically matching the beginning of the line then negating (?!) (negative lookahead) your regex.
You may use a negative lookahead here:
https?:\/\/(?!.*\.google\..*search).*
Demo
The "secret sauce" here is (?!.*\.google\..*search), which asserts that .google. followed by search does not occur anywhere within the URL to the right of the https:// portion.
Related
This question already has answers here:
RegEx to get section of URL after string in Javascript
(2 answers)
Closed 1 year ago.
I’m trying to extract a string between 2 slashes in a url for example:
HTTPS://www.google.com/blabla/ab /what2/what3/lalaalala
I want to extract the ab which is between 2 slashes in the middle. How can I extract it? It always comes after blabla if it helps.
I tried:
([^\/]+$)
You want to use a positive lookbehind for that: (?<=...)
It checks if something exists before your match without capturing it in the result.
Here is the regex:
(?<=\/blabla\/)([^\/]+)
Now you only get ab as a result.
https://regex101.com/r/4gEsfP/1
Note: It's "positive" because it checks whether it is present, and "lookbehind" because it looks behind the ([^\/]+) pattern. There are also negative lookbehinds and positive/negative lookaheads.
Some good resource about it: https://javascript.info/regexp-lookahead-lookbehind
This question already has answers here:
How can I match on, but exclude a regex pattern?
(6 answers)
Closed 2 years ago.
How do i extract this from a regex:
mymatch[ someother char ]
What i want is mymatch when followed by a [ but i don't want the square bracket in the match.
I'm stuck on this but it gets also the square bracket:
\b.*?\[
More in general, how can i exclude from the match some portion of the pattern?
For example here (abc2)mymatch i want a regex returning my match only when it is preeceds by (abc2).
in this you need to use lookahead and lookbehind , in first one use positive lookahead :
\w+(?=\[)
?=[ : mean followed by "[" and not catch in matches
in second example use positive lookbehind :
(?<=\(abc2\))\w+
(?<=(abc2)) : mean leaded by "(abc2)" and not catch in matches
for more information about lookahead and lookbehind
This question already has answers here:
Regex Last occurrence?
(7 answers)
Closed 3 years ago.
I have the following RegEx syntax that will match the first date found.
([0-9]+)/([0-9]+)/([0-9]+)
However, I would like to start from the end of the content and search backwards. In other words, in the below example, my syntax will always match the first date, but I want it to match the last instead.
Some Text here
01/02/15
Some additional
text here.
10/04/14
Ending text
here
I believe this is possible by using a negative lookahead, but all my attempts failed at this because I don't understand RegEx enough. Help would be appreciated.
Note: my application uses RegEx PCRP.
You could make the dot match a newline using for example an inline modifier (?s) and match until the end of the string.
Then make use of backtracking until the last occurrence of the date like pattern and precede the first digit with a word boundary.
Use \K to forget what was matched and match the date like pattern.
^(?s).*\b\K[0-9]+/[0-9]+/[0-9]+
Regex demo
Note that the pattern is a very broad match and does not validate a date itself.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
What is the use of
?=
in perl regex
please tell the exact meaning and give some regex example.
(?=...)
is a positive lookahead, a type of zero-width assertion. What it's saying is that the match must be followed by whatever is within the parentheses but that part isn't captured.
Example:
.*(?=bar)
This pattern matches all the characters upto the string bar. When bar is detected then it stops matching. If a line contains more than one bar means it matches upto the last bar because .* does a greedy match.
DEMO
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular expression to match string not containing a word?
How can I invert a regular expression in JavaScript?
Say I have the regex foo123. How do I match everything that is not foo123?
Use negative lookahead for this.
(?!foo123).+
matches any string except foo123
If you want to match empty string also, use (?!foo123).*
In your case (according to the comment) the required regex is (?!P[0-9]{1,}).+.
It matches P and 123, but not P123.