[SalesForce] Replace String with Wildcard

Is it possible to replaceall on a string using wildcards?
For example

string adder;
adder = '[Test | 123 ! xyz]';
string replaced;
replaced = adder.replaceall('|%!','');

System.AssertEquals(replaced,'Test xyz');

Here I am using % as wildcard but it is not working.

Best Answer

The documentation for the replaceAll() method says that it accepts a Regular Expression as the first argument. If you wanted to get rid of everything between | and ! inclusive you can use adder.replaceAll('(\\|.*!)','');.

Related Topic