简体   繁体   中英

Trying to select preceding zeros of a number present in a RQL query using regex

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, eg

query = "ge(dateCreated,'2013-01-18T19:30:00.000Z')"; 

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM