正则表达式小记

有一个字符串数组如下:

 

String [] str = {"Order-to-Cash : Sales & Distribution","Procure-to Pay : Materials Management","Customer Service & Plant Maintenance","Controlling / Annual Operating Plan & Forecast"};

 

替换掉其中的 "-"," : "," & "," "以及" / "字符,(注意,":","&"以及"/" 前后都有空格)

 

正则表达式如下:

 

String regex = "(-|\\s{1,}:\\s{1,}|\\s{1,}&\\s{1,}|\\s{1,}/\\s{1,}|\\s{1,})";

 

for(String s: str){
            String result = s;
            Pattern pattern = Pattern.compile(regex);
            Matcher m = pattern.matcher(s);
            System.out.println(m.find());
            while(m.find()){
                result = s.replaceAll(regex, "_");
            }
            System.out.println(result);
        }

 

测试结果如下:

 

 

true
Order_to_Cash_Sales_Distribution
true
Procure_to_Pay_Materials_Management
true
Customer_Service_Plant_Maintenance
true
Controlling_Annual_Operating_Plan_Forecast

你可能感兴趣的:(正则表达式)