Aarushi Mishra
Aarushi Mishra

Reputation: 682

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?

Upvotes: 1

Views: 2287

Answers (2)

chandu kavar
chandu kavar

Reputation: 420

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);
    }
}

Upvotes: 1

hwnd
hwnd

Reputation: 70732

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

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

Ideone Demo

Upvotes: 1

Related Questions