user3774445
user3774445

Reputation: 29

How to process a list of patterns using Pattern.compile in java

The code shown here is processing single pattern value. But now i need to process a list of patterns. Can anyone suggest an alternative for Pattern.compile(pattern). Since it processes only a single value. Here patternValue is an arraylist populating from database.

public static String processFuction(String input, String pattern, String replace)
            throws Exception {
        try {
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(input);
            input = m.replaceAll(replace);
        } catch (Exception ex) {
            logger.severe(ex.getMessage());
        }
        return input;
    } 

text = processFuction(text, patternValue,name[0]);

Upvotes: 0

Views: 2974

Answers (1)

Sumit Singh
Sumit Singh

Reputation: 15896

I thinks there is no way in Java API so that you can process a list of patterns.
So better way for you would be take list of pattern and process it accordingly.

EX:

public static String processFuction(String input, List<String> patternList, String replace)
            throws Exception {
  try {
        for (String pattern : patternList){
          Pattern p = Pattern.compile(pattern);
          Matcher m = p.matcher(input);
          input = m.replaceAll(replace);// do your operation 
        }
   } catch (Exception ex) {
       logger.severe(ex.getMessage());
   }
  return input;
} 

Upvotes: 2

Related Questions