简体   繁体   中英

Need help in creating a regular expression in Java

I am trying to apply regex on a string to obtain the output in the below format.

Input string:

params=Param1{index}~[Value1|Value2|....]^Param2~[Value1|Value2|....]

Output should be in the form of Map<String,List<String>> Eg.

Key1: Param1
Value : Value1,Value2 upto ValueN
Key2 :Param2
Value: Value1,Value2 uptoValueN

Code: Her is the code that I am trying to do.

String textPatt = "params";
String key = textPatt.replace("/[\\[]/", "\\[").replace("/[\\]]/", "\\]");
String pattern = new String("[\\?&]" + key + "=([^&#]*)");
 Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(masterUrl);
List<String> paramList = new ArrayList<String>();
if (m.find()) {
String param = m.group();

if(param != null){
    String[] multiParamArr  = null;
    if(param.indexOf("^") != -1){
        multiParamArr = param.split("\\^");


    }else{
        multiParamArr = new String[1];
        multiParamArr[0] = param;
    }
    if(multiParamArr != null && multiParamArr .length > 0){


            Pattern refinePattern = null;
            Matcher refineMatcher = null;
            String paramKeyy = null;
            String paramVal = null;

            HashMap<String,List<String>> ParamMap = new HashMap<String,List<String>>();
             List<String list = new ArrayList<String>();
            for(int i=0;i<multiParamArr.length;i++){
                String[] multiParam = null;
                 String patternKey = "^[^\\~]*";
                 refinePattern = Pattern.compile(patternKey);
                 refineMatcher = refinePattern.matcher(multiRefineArr[i]);
                 if(refineMatcher.find()){
                 paramKey = refineMatcher.group();
                  if(paramKey.indexOf("=") !=-1){
                      patternKey  = patternKey.substring(patternKey.indexOf("=")+1);
                  }
                  if(paramKey.indexOf("{") !=-1){
                      patternKey  = patternKey.substring(patternKey.indexOf("=")+1,patternKey.indexOf("{"));
                  }
                 }
                    if(multiParamArr[i].indexOf("|") != -1){
                         multiParam = multiParamArr[i].split("\\|");
                         int startIndex;
                         int endIndex;

                        for(int j=0;j<multiParam.length;j++){
                            startIndex = 0;
                            endIndex = 0;
                            ifmultiParamArr[j].indexOf("[") > -1){
                                 startIndex = multiParamArr[j].indexOf("[")+1;
                            }
                            ifmultiParamArr[j].indexOf("]") > -1){
                                endIndex = multiParamArr[j].indexOf("]");

                            }else{
                                endIndex = multiParamArr[j].length();
                            }
                        paramVal = multiParamArr[j].substring(startIndex,endIndex);

                        }
                    }else{
                        paramVal = multiParamArr[i].substring(multiParamArr[i].indexOfmultiParamArr[i].indexOf("]"));

                    }
                    list.add(paramVal);

                }
   paramMap.put((paramKey,list);
    }


}

} how to apply a single regex and achieve the output

Instead of trying to create a complex regex and maintain it, you can do it simpler and in a more readable way, in a few steps:

  1. Split on ^
  2. Now you have an array of Strings, each of which contains one of the parameters with its values
  3. Split each string on ~ - you'll get an array of two strings, extract from the first the Param1 ( Param2 etc)
  4. From the second you can extract the list of values by splitting on | (pipe).

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