Java RegEx用于替换开头和结尾部分与特定模式匹配的字符串

时间:2011-12-30 17:46:42

标签: java regex

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

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

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

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

对此的想法:

  1. String str = xmlstring.replaceAll("wsp:rsid/w", "");
  2. String str = xmlstring.replaceAll("wsp:rsid[]\\"", "");

4 个答案:

答案 0 :(得分:2)

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, "" ) );
}

答案 1 :(得分:1)

尝试:

xmlstring.replaceAll("\\bwsp:rsid\\w*=\"[^\"]+(\\\\\"[^\"]*)*\"", "");

另外,你的正则表达式错了。我建议你去看看http://regular-expressions.info;)

答案 2 :(得分:0)

这是2个功能。 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));
        }

    }   

}

返回:

sadfasdfchri  foo="bar"

Key: RDefault, Value: 005816D6

Key: P, Value: 005816D6

Key: R, Value: 005816D6

答案 3 :(得分:0)

与其他所有答案不同,这个答案确实有效!

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>