我需要一個正則表達式來僅從xml標籤中刪除uri前綴(標籤內)。正則表達式只能從xml標籤中刪除uri前綴(標籤內)
例
輸入:
<ns1:fso xlmns:="http://xyz"><sender>abc</sender></ns1:fso>
輸出:
<fso xlmns:="http://xyz"><sender>abc</sender></fso>
這裏是我的代碼:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class RegularExpressionTest {
private static String REGEX1 = "<\\/?([a-z0-9]+?:).*?>";
private static String INPUT = "<ns1:fso xmlns:ns1='https://www.example.com/fsoCanonical'>
<ns2:senderId xmlns='http://www.example.com/fsoCanonical'>abc</ns2:senderId>
<receiverId xmlns='http://www.example.com/fsoCanonical'>testdata</receiverId>
<messageId xmlns='http://www.example.com/fsoCanonical'>4CF4DC05126A0077E10080000A66C871</messageId>
</ns1:fso> ";
private static String REPLACE = "";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX1);
Matcher m = p.matcher(INPUT); // get a matcher object
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, REPLACE);
}
m.appendTail(sb);
System.out.println(sb.toString());
}
我沒有噸能夠在這裏
private static String INPUT =
粘貼輸入XML不是如在上面的代碼中所示的正確的。相反,你可以採取任何肥皂消息的例子。
我必須寫在Java我不是很好的正則表達式...感謝在adavance ... – samash 2011-05-12 07:30:27
它匹配得很好'ns1:' - 所以你只需要將其替換爲空字符串。 – hsz 2011-05-12 08:50:20
發佈你的代碼,你已經這樣做(編輯你的問題 - 而不是評論)。 – hsz 2011-05-12 09:27:51