Java正则表达式,用于替换开头和结尾部分与特定模式匹配的字符串

Java正则表达式,用于替换开头和结尾部分与特定模式匹配的字符串,java,regex,Java,Regex,您好,我需要用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:

您好,我需要用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[]\\”“,”)
  • 尝试:

    而且,您的正则表达式是错误的。我建议你去苦干一番;)

    这在我的测试中起作用

    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, "" ) );
    }
    
    public void testReplaceAll()引发异常{
    字符串regex=“wsp:rsid\\w*?=\”*?\”;
    assertEquals(“,”wsp:rsidP=\“005816D6\\”.replaceAll(regex“”);
    assertEquals(“,”wsp:rsidR=\“005816D6\”.replaceAll(正则表达式“)”);
    assertEquals(“,”wsp:rsidRDefault=\“005816D6\”.replaceAll(regex“”);
    assertEquals(“a=”1\“>”,“a=”1\”wsp:rsidP=”005816D6\“>”.replaceAll(正则表达式“”);
    资产质量(
    “鲍勃·库哈尔”,
    “bob wsp:rsidP=\'005816D6\'wsp:rsidrfault=\'005816D6\'kuhar.replaceAll(regex,”);
    资产质量(
    “keepme=”是“,
    “wsp:rsidP=\”005816D6\”keepme=\”yes\”wsp:rsidR=\”005816D6\”。replaceAll(正则表达式“”);
    资产质量(
    "",
    “.replaceAll(regex)”;
    //可悲的是,它没有处理嵌入的\“案例。。。
    //assertEquals(“,”wsp:rsidR=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
    }
    
    这里有两个函数。clean将进行替换,extract将提取数据(如果需要,不确定)

    请原谅我的风格,我希望你能够剪切和粘贴的功能

    import java.util.HashMap;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    public class Answer {
    
        public static HashMap<String, String> extract(String s){
            Pattern pattern  = Pattern.compile("wsp:rsid(.+?)=\"(.+?)\"");
            Matcher matcher = pattern.matcher(s);
            HashMap<String, String> hm = new HashMap<String, String>();
    
            //The first group is the string between the wsp:rsid and the =
            //The second is the value
            while (matcher.find()){
                hm.put(matcher.group(1), matcher.group(2));
            }
    
            return hm;
        }
    
        public static String clean(String s){
            Pattern pattern  = Pattern.compile("wsp:rsid(.+?)=\"(.+?)\"");
            Matcher matcher = pattern.matcher(s);
            return matcher.replaceAll("");
        }
    
        public static void main(String[] args) {
    
            System.out.print(clean("sadfasdfchri wsp:rsidP=\"005816D6\" foo=\"bar\" wsp:rsidR=\"005816D6\" wsp:rsidRDefault=\"005816D6\""));
            HashMap<String, String> m = extract("sadfasdfchri wsp:rsidP=\"005816D6\" foo=\"bar\" wsp:rsidR=\"005816D6\" wsp:rsidRDefault=\"005816D6\"");
            System.out.println("");
    
            //ripped off of http://stackoverflow.com/questions/1066589/java-iterate-through-hashmap
            for (String key : m.keySet()) {
                System.out.println("Key: " + key + ", Value: " + m.get(key));
            }
    
        }   
    
    }
    

    与所有其他答案不同,这个答案实际上是有效的

    xmlstring.replaceAll("\\bwsp:rsid\\w*?=\"[^\"]*\"", "");
    
    以下是一个测试,所有其他答案都失败了:

    public static void main(String[] args) {
        String xmlstring = "<tag wsp:rsidR=\"005816D6\" foo=\"bar\" wsp:rsidRDefault=\"005816D6\">hello</tag>";
        System.out.println(xmlstring);
        System.out.println(xmlstring.replaceAll("\\bwsp:rsid\\w*?=\"[^\"]*\"", ""));
    }
    
    publicstaticvoidmain(字符串[]args){
    String xmlstring=“hello”;
    System.out.println(xmlstring);
    System.out.println(xmlstring.replaceAll(“\\bwsp:rsid\\w*?=\”[^\“]*\”,”);
    }
    
    输出:

    <tag wsp:rsidR="005816D6" foo="bar" wsp:rsidRDefault="005816D6">hello</tag>
    <tag  foo="bar" >hello</tag>
    
    你好 你好
    你是说
    “\\bwsp:rsid=\”[^\“]+\”“
    ?不是,因为
    rsid
    后面可以跟
    R
    。让我这样说吧。。。你的正则表达式不起作用。你自己测试看看。你的正则表达式也不能与wsp:rsidR=“hello\”world“一起使用。但是,我编辑的正则表达式将.FYI与
    “hello\”world“
    一起使用,你需要一个XML解析器,这超出了这个问题的范围。看看为什么这是一个可怕的解决方案。。。“更多代码”并不意味着“更好的代码”。“正确”的答案是一个内衬。大多数是锅炉板。当然答案是一行,实际上只有一个正则表达式。答案的正确实现不是一行代码。我们不知道那是什么。我提供了我的,现在可以用了。regex根据波希米亚人嘲笑er的建议变得不贪婪。当a)你删除了你的答案,b)你修复了你的答案(第一个regex仍然是旧的破答案)我的答案有效,或者至少满足了原始问题的要求时,我会删除-1。我认为没有必要对其进行编辑。我真的不太在乎-1。所以我不会删除-1,除非答案需要编辑。编辑一下,我把它放回去。我仍然认为你的风格缺乏客观性,这使得它很好地发挥作用。
    xmlstring.replaceAll("\\bwsp:rsid\\w*?=\"[^\"]*\"", "");
    
    public static void main(String[] args) {
        String xmlstring = "<tag wsp:rsidR=\"005816D6\" foo=\"bar\" wsp:rsidRDefault=\"005816D6\">hello</tag>";
        System.out.println(xmlstring);
        System.out.println(xmlstring.replaceAll("\\bwsp:rsid\\w*?=\"[^\"]*\"", ""));
    }
    
    <tag wsp:rsidR="005816D6" foo="bar" wsp:rsidRDefault="005816D6">hello</tag>
    <tag  foo="bar" >hello</tag>