Filtering Unwanted Character Strings in Java

I iterate through hundreds of data records, most of them are valid according to my rules, but there are some special characters or unwanted spaces that need to be filtered out before the record is used.

I only need the symbols =and ,, which can be indicated with numbers and letters. No other special characters. There may be separate spaces, but ONLY after ,to separate the data.

I call the filter method inside the loop:

private String filterText(String textToBeFiltered) {
    String filteredText = null;

    // Remove all chars apart from = and , with whitespace only allowed
    // after the ,

    return filteredText;
}

I am completely new to regex, but etching textbooks and would appreciate any ideas.

Thank!

Franc

+3
1

replaceAll :

input = input.replaceAll("[^=,\\da-zA-Z\\s]|(?<!,)\\s","");

: [^=,\\da-zA-Z\\s]|(?<!,)\\s, :

  • , =, , "", .
  • , ,
+7

Source: https://habr.com/ru/post/1777651/


All Articles