[英]Regex: match everything the other regex left
I am struggling with the following issue: say there's a regex 1 and there's regex 2 which should match everything the regex 1 does not. 我正在努力解决以下问题:说有一个正则表达式1并且正则表达式2应该匹配正则表达式1所没有的一切。
Let's have the regex 1: /\\$\\d+/
(ie the dollar sign followed by any amount of digits. 让我们有正则表达式1: /\\$\\d+/
(即美元符号后跟任意数量的数字。
Having a string like foo$12___bar___$34wilma buzz
it detects $12
and $34
. 有一个字符串像foo$12___bar___$34wilma buzz
它检测到$12
$34
和$34
。
How does the regex 2 should look in order to match the remained parts of the aforementioned string, ie foo
, ___bar___
and wilma buzz
? 正则表达式2应该如何匹配上述字符串的剩余部分,即foo
, ___bar___
和wilma buzz
? In other words it should pick up all the "remained" chunks of the source string. 换句话说,它应该拾取源字符串的所有“剩余”块。
You may use String#split
to split on given regex and get remaining substrings in an array: 您可以使用String#split
在给定的正则表达式上拆分并在数组中获取剩余的子串:
String[] arr = str.split( "\\$\\d+" );
//=> ["foo", "___bar___", "wilma buzz"]
It was tricky to get this working, but this regex will match everything besides \\$\\d+
for you. 让这个工作很棘手,但这个正则表达式将匹配除了\\$\\d+
之外的所有内容。 EDIT: no longer erroneously matches $44$444
or similar. 编辑:不再错误地匹配$44$444
或类似。
(?!\$\d+)(.+?)\$\d+|\$\d+|(?!\$\d+)(.+)
Breakdown
(?!\$\d+)(.+?)\$\d+
(?! ) negative lookahead: assert the following string does not match
\$\d+ your pattern - can be replaced with another pattern
(.+?) match at least one symbol, as few as possible
\$\d+ non-capturing match your pattern
OR
\$\d+ non-capturing group: matches one instance of your pattern
OR
(?!\$\d+)(.+)
(?!\$\d+) negative lookahead to not match your pattern
(.+) match at least one symbol, as few as possible
GENERIC FORM
(?!<pattern>)(.+?)<pattern>|<pattern>|(?!<pattern>)(.+)
By replacing <pattern>
, you can match anything that doesn't match your pattern. 通过替换<pattern>
,您可以匹配任何与您的模式不匹配的内容。 Here's one that matches your pattern, and here's an example of arbitrary pattern (un)matching. 这是一个匹配您的模式,这是一个任意模式(un)匹配的例子。
Good luck! 祝好运!
Try this one 试试这个吧
[a-zA-Z_]+
Or even better 甚至更好
[^\$\d]+ -> With the ^symbol you can negotiate the search like ! in the java -> not equal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.