可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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\"
?
回答1:
You didn\'t specify which flavor of regex you\'re using, but this will
work in any of the most popular ones that can be considered \"complete\".
/.+?(?=abc)/
How it works
The .+?
part is the un-greedy version of .+
(one or more of
anything). When we use .+
, the engine will basically match everything.
Then, if there is something else in the regex it will go back in steps
trying to match the following part. This is the greedy behavior,
meaning as much as possible to satisfy.
When using .+?
, instead of matching all at once and going back for
other conditions (if any), the engine will match the next characters by
step until the subsequent part of the regex is matched (again if any).
This is the un-greedy, meaning match the fewest possible to
satisfy.
/.+X/ ~ \"abcXabcXabcX\" /.+/ ~ \"abcXabcXabcX\"
^^^^^^^^^^^^ ^^^^^^^^^^^^
/.+?X/ ~ \"abcXabcXabcX\" /.+?/ ~ \"abcXabcXabcX\"
^^^^ ^
Following that we have (?=
{contents}
)
, a zero width
assertion, a look around. This grouped construction matches its
contents, but does not count as characters matched (zero width). It
only returns if it is a match or not (assertion).
Thus, in other terms the regex /.+?(?=abc)/
means:
Match any characters as few as possible until a \"abc\" is found,
without counting the \"abc\".
回答2:
If you\'re looking to capture everything up to \"abc\":
/^(.*?)abc/
Explanation:
( )
capture the expression inside the parentheses for access using $1
, $2
, etc.
^
match start of line
.*
match anything, ?
non-greedily (match the minimum number of characters required) - [1]
[1] The reason why this is needed is that otherwise, in the following string:
whatever whatever something abc something abc
by default, regexes are greedy, meaning it will match as much as possible. Therefore /^.*abc/
would match \"whatever whatever something abc something \". Adding the non-greedy quantifier ?
makes the regex only match \"whatever whatever something \".
回答3:
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.
回答4:
What you need is look around assertion like .+? (?=abc)
.
See: Lookahead and Lookbehind Zero-Length Assertions
Be aware that [abc]
isn\'t the same as abc
. Inside brackets it\'s not a string - each character is just one of the possibilities. Outside the brackets it becomes the string.
回答5:
This will make sense about regex.
- The exact word can be get from the following regex command:
(\"(.*?)\")/g
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.
回答6:
For regex in Java, and I believe also in most regex engines, if you want to include the last part this will work:
.+?(abc)
For example, in this line:
I have this very nice senabctence
select all characters until \"abc\" and also include abc
using our regex, the result will be: I have this very nice senabc
Test this out: https://regex101.com/r/mX51ru/1
回答7:
I believe you need subexpressions. If I remember right you can use the normal ()
brackets for subexpressions.
This part is From grep manual:
Back References and Subexpressions
The back-reference \\n, where n is a single digit, matches the substring
previously matched by the nth parenthesized subexpression of the
regular expression.
Do something like ^[^(abc)]
should do the trick.
回答8:
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.
回答9:
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 of abc
, but it would have to be at the end
Also 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).
回答10:
try this
.+?efg
Query :
select REGEXP_REPLACE (\'abcdefghijklmn\',\'.+?efg\', \'\') FROM dual;
output :
hijklmn