gpt4 book ai didi

Java RegEx 替换开始和结束部分匹配特定模式的字符串

转载 作者:行者123 更新时间:2023-12-04 06:03:16 25 4
gpt4 key购买 nike

嗨,我需要在 java 中编写一个正则表达式,它将找到以下所有实例:

wsp:rsidP="005816D6" wsp:rsidR="005816D6" wsp:rsidRDefault="005816D6" 

XML 字符串中的属性并将其删除:

所以我需要删除所有以 wsp:rsid 开头的属性并以双引号结尾 ( " )

对此的想法:
  • String str = xmlstring.replaceAll("wsp:rsid/w", "");
  • String str = xmlstring.replaceAll("wsp:rsid[]\\"", "");
  • 最佳答案

    xmlstring.replaceAll( "wsp:rsid\\w*?=\".*?\"", "" );

    这在我的测试中有效...
    public void testReplaceAll() throws Exception {
    String regex = "wsp:rsid\\w*?=\".*?\"";

    assertEquals( "", "wsp:rsidP=\"005816D6\"".replaceAll( regex, "" ) );
    assertEquals( "", "wsp:rsidR=\"005816D6\"".replaceAll( regex, "" ) );
    assertEquals( "", "wsp:rsidRDefault=\"005816D6\"".replaceAll( regex, "" ) );
    assertEquals( "a=\"1\" >", "a=\"1\" wsp:rsidP=\"005816D6\">".replaceAll( regex, "" ) );
    assertEquals(
    "bob kuhar",
    "bob wsp:rsidP=\"005816D6\" wsp:rsidRDefault=\"005816D6\" kuhar".replaceAll( regex, "" ) );
    assertEquals(
    " keepme=\"yes\" ",
    "wsp:rsidP=\"005816D6\" keepme=\"yes\" wsp:rsidR=\"005816D6\"".replaceAll( regex, "" ) );
    assertEquals(
    "<node a=\"l\" b=\"m\" c=\"r\">",
    "<node a=\"l\" wsp:rsidP=\"0\" b=\"m\" wsp:rsidR=\"0\" c=\"r\">".replaceAll( regex, "" ) );
    // Sadly doesn't handle the embedded \" case...
    // assertEquals( "", "wsp:rsidR=\"hello\\\"world\"".replaceAll( regex, "" ) );
    }

    关于Java RegEx 替换开始和结束部分匹配特定模式的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8682299/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com