I'm trying to remove the preceding zeros of numbers present in below query
String query = "or(contains(number,'04506'),contains(name,'04506'),contains(vendorInfo.name,'04506'),contains(vendorInfo.number,'04506'),contains(costCategories.name,'04506')";
I'm using below regex
query = query.replaceAll("\\b0 ","");
Problem is, it also removes zero, if we provide a query with a timestamp to it, e.g.
query = "ge(dateCreated,'2013-01-18T19:30:00.000Z')";
CodePudding user response:
You can use negative lookarounds to not remove the zero followed by or preceded by any of -
, :
, .
or T
.
class Main {
public static void main(String args[]) {
String query = "contains(costCategories.name,'05.04506')ge(dateCreated,'2013-01-18T09:30:00.000Z')";
query = query.replaceAll("(?<![-:\\.T])\\b0 (?![-:\\.T])", "");
System.out.println(query);
}
}
Output:
contains(costCategories.name,'5.04506')ge(dateCreated,'2013-01-18T09:30:00.000Z')