Take this regular expression: /^[^abc]/
. This will match any single character at the beginning of a string, except a, b, or c.
If you add a *
after it – /^[^abc]*/
– the regular expression will continue to add each subsequent character to the result, until it meets either an a
, or b
, or c
.
For example, with the source string "qwerty qwerty whatever abc hello"
, the expression will match up to "qwerty qwerty wh"
.
But what if I wanted the matching string to be "qwerty qwerty whatever "
...In other words, how can I match everything up to (but not including) the exact sequence "abc"
?
I ended in this stackoverflow question after looking for help to solve my problem but found no solution to it :(
So I had to improvise... after some time I managed to reach the regex I needed:
As you can see, I needed up to one folder ahead of "grp-bps" folder, without including last dash. And it was required to have at least one folder after "grp-bps" folder.
I believe you need subexpressions. If I remember right you can use the normal
()
brackets for subexpressions.This part is From grep manual:
Do something like
^[^(abc)]
should do the trick.The
$
marks the end of a string, so something like this should work:[[^abc]*]$
where you're looking for anything NOT ENDING in any iteration ofabc
, but it would have to be at the endAlso if you're using a scripting language with regex (like php or js), they have a search function that stops when it first encounters a pattern (and you can specify start from the left or start from the right, or with php, you can do an implode to mirror the string).
As @Jared Ng and @Issun pointed out, the key to solve this kind of RegEx like "matching everything up to a certain word or substring" or "matching everything after a certain word or substring" is called "lookaround" zero-length assertions. Read more about them here.
In your particular case, it can be solved by a positive look ahead. A picture is worth a thousand words. See the detail explanation in the screenshot.
try this
Query :
output :
This will make sense about regex.
Here, we can get the exact word globally which is belonging inside the double quotes. For Example, If our search text is,
This is the example for "double quoted" words
then we will get "double quoted" from that sentence.