java - Get what was removed by String.replaceAll() -


so, let's got regular expression

string regex = "\d*"; 

for finding digits.

now got inputted string, example

string input = "we got 34 apples , do"; 

now want replace digits "", doing that:

input = input.replaceall(regex, ""); 

when printing input got "we got apples , do". works, replaced 3 , 4 "".

now question: there way - maybe existing lib? - replaced?

the example here simple, understand how works. want use complexer inputs , regex.

thanks help.

you can use matcher append-and-replace procedure:

string regex = "\\d*";  pattern pattern = pattern.compile(regex); matcher matcher = pattern.matcher(input);  stringbuffer sb = new stringbuffer(); stringbuffer replaced = new stringbuffer(); while(matcher.find()) {     replaced.append(matcher.group());     matcher.appendreplacement(sb, ""); } matcher.appendtail(sb);  system.out.println(sb.tostring());  // prints replacement result system.out.println(replaced.tostring()); // prints replaced 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -