我有这个字符串:
<meis xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" uri="localhost/naro-nei" onded="flpSW531213" identi="lemenia" id="75" lastStop="bendi" xsi:noNamespaceSchemaLocation="http://localhost/xsd/postat.xsd xsd/postat.xsd">
如何在 JAVA 中获取 lastStop
属性值?
此正则表达式在 http://www.myregexp.com/ 上测试时有效
但是当我在 java 中尝试时,我没有看到匹配的文本,这是我的尝试方式:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class SimpleRegexTest {
public static void main(String[] args) {
String sampleText = "<meis xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" uri=\"localhost/naro-nei\" onded=\"flpSW531213\" identi=\"lemenia\" id=\"75\" lastStop=\"bendi\" xsi:noNamespaceSchemaLocation=\"http://localhost/xsd/postat.xsd xsd/postat.xsd\">";
String sampleRegex = "(?<=lastStop=[\"']?)[^\"']*";
Pattern p = Pattern.compile(sampleRegex);
Matcher m = p.matcher(sampleText);
if (m.find()) {
String matchedText = m.group();
System.out.println("matched [" + matchedText + "]");
} else {
System.out.println("didn’t match");
}
}
}
也许问题是我在测试中使用了转义符,但真正的字符串内部没有转义符。 ?
更新
有谁知道为什么这在 java 中使用时不起作用?或者如何让它发挥作用?
最佳答案
(?<=lastStop=[\"']?)[^\"]+
关于java - java读取字符串部分的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2636056/