Regex: match everything the other regex left

varnie :

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.

Let's have the regex 1: /\$\d+/ (i.e. the dollar sign followed by any amount of digits.

Having a string like foo$12___bar___$34wilma buzz it detects $12 and $34.

How does the regex 2 should look in order to match the remained parts of the aforementioned string, i.e. foo, ___bar___ and wilma buzz? In other words it should pick up all the "remained" chunks of the source string.

anubhava :

You may use String#split to split on given regex and get remaining substrings in an array:

String[] arr = str.split( "\\$\\d+" );

//=> ["foo", "___bar___", "wilma buzz"]

RegEx Demo

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=141170&siteId=1