+ Start a Discussion
Vijay NagarathinamVijay Nagarathinam 

Regular expression issue

Hi All,

What is the purpose of the below regular expression?

string test = '00045304^+453534$!';
system.debug('>>>> Remove leading zero >>'+test.trim().replaceAll('^0+(?!$)', ''));

Thanks,
Vijay
 
Ankur Saini 9Ankur Saini 9
Hi Vijay,

If you have to remove all special characters from this string, use : 
string test = '00045304^+453534$!';
system.debug('>>>> Remove leading zero >>'+test.replaceAll('[^a-zA-Z0-9]', ''));
or
system.debug('>>>> Remove leading zero >>'+test.trim().replaceAll('[0+(?!$)]', ''));

If you have to remove all alphanumeric characters from this string, use : 
string test = '00045304^+453534$!';
system.debug('>>>> Remove leading zero >>'+test.replaceAll('[a-zA-Z0-9]', ''));
or
system.debug('>>>> Remove leading zero >>'+test.trim().replaceAll('[^0+(?!$)]', ''));

Thanks,
Ankur Saini
http://mirketa.com