Regular expression to replace specific numbers to space

Given:

a = 0145067168 b = 0245067167 c = 0345067166 d = 0445067165 e = 0545067184  z = 2545067168 

How do I remove leading indicators ("01", "02", "03","04","05"), using regular expression

This is the output I’m expecting:

45067168 45067167 45067166 45067165 45067184  2545067168 

Any help would be great thanks.

Add Comment
2 Answer(s)

Why use regex here? Regex are cpu intensive. Convert it to string and use substring String.valueOf(a).substring(2);

Answered on July 15, 2020.
Add Comment

You can use replaceFirst() method to remove the values ("01", "02", "03","04","05")

replaceFirst(String regex, String replacement) Replaces the first substring of this string that matches the given regular expression with the given replacement.

Try this:

String a = "0145067168"; String output = a.replaceFirst("01|02|03|04|05", ""); System.out.println(output); 

Output:

45067168 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.