【问题标题】:Regular expression replace from founded keyword to next &正则表达式从founded关键字替换为next &
【发布时间】:2015-02-02 20:23:11
【问题描述】:

我不是没有任何代码的问题的朋友,但我完全不知道如何搜索这个问题。

我得到了一个这样的字符串,其中包含几个键/值对:test=39&test2=4&test3=10

还有一个像test2=5这样的更新字符串

现在我想用一个表达式调用String.replaceAll 方法,用test2=5 替换上面的字符串,所以我得到test=39&test2=5&test3=10

String lTest = "test=39&test2=4&test3=10";
String lUpdate = "test2=5";
Assert.assertEquals("test=39&test2=5&test3=10",lTest.replace(lTest,???regex???);

lUpdate 可以是test 或test2 或test3。 = 后面的值可以是 0 到 1000 之间的数字。

如何使用正则表达式来管理?

非常感谢您的帮助。

更新!!! 这就像一个魅力(感谢用户 laune):

@Test
public void test(){
  String lResult = update("test1=1234&test2=4949&test3=1", "test2=42");
  assertEquals("test1=1234&test2=42&test3=1", lResult);

  lResult = update("test1=1234&test2=4949&test3=1", "test3=5");
  assertEquals("test1=1234&test2=4949&test3=5", lResult);

  lResult = update("test1=1234&test2=4949&test3=1", "test1=100");
  assertEquals("test1=100&test2=4949&test3=1", lResult);
}

static String update(String str, String upd) {
  // split to get the keyword and the value
  String[] kwv = upd.split("=");
  // compose the regex
  String regex = "(?<=^|&)" + Pattern.quote(kwv[0]) + "=\\d+";
  return str.replaceAll(regex, upd);
}

【问题讨论】:

  • s.replace( "test=39&amp;test2=4&amp;test3=10", "test=39&amp;test2=5&amp;test3=10") 甚至 s.replace( "test2=4&amp;", "test2=5&amp;") 都会按照您的描述进行操作。如果这不是您想要的,您将不得不改进您的描述。 (什么是变量,什么不是?)
  • 总是中间值吗?
  • 查看我编辑的答案
  • 请参阅Query String Manipulation in Java,因为您似乎正在使用查询字符串,而我不知道 JAVA,但我知道您需要更像对象而不是字符串来进行此类数据操作。跨度>

标签: java regex


【解决方案1】:

应该从更新字符串构造正则表达式。我使用否定的lookbehind,这样即使是带有嵌入连字符的关键字也可以使用。

static String update( String str, String upd ){
    // split to get the keyword and the value
    String[] kwv = upd.split( "=" );
    // compose the regex
    String regex = "(?<=^|&)" + Pattern.quote(kwv[0]) + "=\\d+";
    return str.replaceAll( regex, upd );
}

【讨论】:

    【解决方案2】:

    创建一个看起来像的新字符串 lTest "\n" lUpdate
    然后使用这个模式

    ^([^=]+)=(\d+)\n(.*?)\1=\d+  
    

    并替换为/

    $3$1=$2  
    

    Demo

    【讨论】:

      【解决方案3】:

      您首先必须将lUpdate 字符串分解为变量名和值,这样您就可以在lTest 中搜索变量名:

      String lTest = "test=39&test2=4&test3=10";
      String lUpdate = "test2=5";
      
      Matcher assignmentMatcher = Pattern.compile("(.*)=(.*)").matcher(lUpdate);
      if (assignmentMatcher.matches()) {
          String varName = assignmentMatcher.group(1);
          String val = assignmentMatcher.group(2);
      
          String replaced = lTest.replaceFirst(varName + "=[^&]+", varName + "=" + val);
          Assert.assertEquals("test=39&test2=5&test3=10",replaced);
      }
      

      【讨论】:

        【解决方案4】:

        这样的事情怎么样

        String lTest = "test=39&test2=4&test3=10";
        String lUpdate = "test2=5";
        
        String name = lUpdate.split("=")[0]; //test2
        lTest = lTest.replaceAll("\\b"+name+"=\\d+", lUpdate); 
        

        因为name 将是test2 正则表达式看起来像\btest2=\d+。

        • \b 是 word boundary,这将阻止正则表达式引擎在 Footest2 中匹配 test2
        • \d+ 代表一个或多个数字,因此它应该能够匹配您问题中指出的 0-1000 范围内的数字。

        如果name=value 对中的name 可能包含一些特殊字符,例如+ 或%,它们也可能是正则表达式元字符,您应该转义它们。最简单的方法是使用Pattern.quote(name)。
        像% 这样的非字母字符(或数字)也将打破与单词边界\b 的接近,因为\btest2 也可以匹配foo%test2,因为% 不被视为单词字符。在这种情况下,我们需要更精确的测试来检查 name 是放在 &amp; 字符之后还是 start of the string (^) 之后。也就是说我们需要使用look-behind(?&lt;=&amp;|^)

        所以你的代码可以像

        lTest = lTest.replaceAll("(?<=&|^)"+Pattern.quote(name)+"=\\d+", lUpdate); 
        

        【讨论】:

        • 在插值变量名称周围添加 \Q 和 \E 定界符以提高稳健性可能也是一个好主意。 (如果变量本身包含特殊的正则表达式符号)。
        • @user268396 \Q 和 \E 也可能有一些问题,如果名称以 \E 开头并以 \Q 结尾。 Pattern.quote(name) 会更好,但由于我不知道输入是来自键盘还是来自预定义的数据,所以我决定暂时跳过这部分。
        • 同意,Pattern.quote() 是要走的路。
        • 很遗憾地说您的解决方案不一致。您为匹配“关键字”中的文字魔法(如 .*?+)而费尽心思,但随后您可能会冒很多非 \w 字符的风险,例如 -:!%,这些字符很可能会出现。
        猜你喜欢
        • 2016-08-16
        • 2012-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-30
        • 1970-01-01
        • 2018-07-13
        相关资源
        最近更新 更多