简体   繁体   中英

Regular Expression not matching in java replaceAll() method

I have an input string -

f.dollar_sales,f.unit_sales

I want to use String.replaceAll(regex,regex) method to get an output string as follows:

dollar_sales,unit_sales

I used the following:

fieldList.replaceAll("[a-zA-Z]\\Q.\\E"," ");

where fieldList is String variable where I've stored input String.

Can someone point out where I've made a mistake?

You need to assign your replaced string and \\Q .. \\E is not necessary here.

fieldList = fieldList.replaceAll("[a-zA-Z]\\.", "");

Ideone Demo

String is immutable, so you need to assign the updated string to that variable.

please run the below code;

public class StringReplace {
    public static void main(String[] args){
        String fieldList="f.dollar_sales,f.unit_sales";
        fieldList=fieldList.replaceAll("[a-zA-Z]\\Q.\\E"," ");
        System.out.println(fieldList);
    }
}

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