正则表达式查找/替换字符串

时间:2017-05-19 15:34:37

标签: java regex replace key-value

我正在尝试使用'userData'来解决问题,该'userData'包含适合regex $ {parameter}的可替换参数,并跳过其他形式(如$ parameter)导致其缺少波形括号。我也跟踪它可以在键盘(kvPairs)中找到的可替换的成功匹配。我想在这种情况下发出警告。

在userData字符串中,我还有一个参数,该参数被包含匹配的值替换。我会认为匹配位置不会被评估,但它会引发异常:

总的来说,我的做法如何?我能改进吗?

exception in thread "main" java.lang.IllegalArgumentException: No group with name {myVariable}
    at java.util.regex.Matcher.appendReplacement(Matcher.java:849)
    at java.util.regex.Matcher.replaceFirst(Matcher.java:1004)
    at 
utils.Regex.getKeyValueSubstitutedUserData(Regex.java:36)
    at utils.Regex.main(Regex.java:77)

我可能决定阻止这个。但我想知道这是怎么回事。是重置位置并重新评估匹配?

package Utils;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex {

   public Regex() {}

   private String getKeyValueSubstitutedUserData(Map<String, String> kvPairs, String userData) {

       String patternToFind = "(\\$\\{)(.+?)(\\})";
       Pattern regex = Pattern.compile( patternToFind );

       Matcher match = regex.matcher( userData );
       Set<String> missingKeys = new LinkedHashSet<>();
       while ( match.find()) {

           String key2 = match.group(2);

           if ( key2 != null ) {

              String value = kvPairs.get( key2 );

              if ( value != null ) {
                  userData = match.replaceFirst( value );
                  match.reset( userData );
              }
              else {
                  // Found a missing variable
                  missingKeys.add( key2 );
              }
          }
      }


      return userData;
  }

  public static void main(String[] args) {

      Regex regexUtil = new Regex();

      Map<String, String> kvPairs = new HashMap<> ();
      kvPairs.put( "LOCALHOSTIP", "10.234.158.5" );
      kvPairs.put( "METADATA_POLL_FREQ" , "60");
      kvPairs.put( "router1_eth0_cidr" , "10.234.158.0/24");
      kvPairs.put( "router1_eth0_gateway" , "10.234.158.1");
      kvPairs.put( "router1_eth0_mac_addresss" , "fa:16:3e:6f:4a:c3");
      kvPairs.put( "EVENT_TYPE" , "BOOT");
      kvPairs.put( "testFIP" , "_FIP_router_eth0");
      kvPairs.put( "METADATA_TIMESTAMP" , "1");
      kvPairs.put( "vnfInstanceNumber" , "1");
      kvPairs.put( "TestFIPVIP" , "_FIP_VIP_router_eth0");
      kvPairs.put( "router1_eth0" , "10.234.158.5");
      kvPairs.put( "myVariable" , "193.167.1.1");
      kvPairs.put( "HOSTNAME" , "router1");
      kvPairs.put( "METADATA_SCRIPT" , "erg");
      kvPairs.put( "_FIP_router1_eth0" , "10.2.31.42");
      kvPairs.put( "LOCALHOST" , "router1");
      kvPairs.put( "testVIP" , "_VIP_router_eth0");
      kvPairs.put( "HOSTNAMEIP" , "10.234.158.5");
      kvPairs.put( "TestNesting" , "${myVariable}");

      String userData = "abcShell script \n${HOSTNAMEIP} blah blah \n ${\nrouter1_eth0_gateway} ${HOSTNAME} $HOSTNAME ${EVENT_TYPE} ${foobar} ${TestNesting} ${_FIP_router1_eth0}";

      regexUtil.getKeyValueSubstitutedUserData( kvPairs, userData);

    }
}

0 个答案:

没有答案