Java 如何检查某个模式是否在xml响应的字符串表示中?
我有以下代码:Java 如何检查某个模式是否在xml响应的字符串表示中?,java,regex,Java,Regex,我有以下代码: Matcher title = Pattern.compile("<Title> (.+?)</Title>").matcher(epg); // for new dongle setup //Matcher title = Pattern.compile("<Title> \"(.+?)\"</Title>").matcher(epg); // for old dongle setup Matcher title
Matcher title = Pattern.compile("<Title> (.+?)</Title>").matcher(epg); // for new dongle setup
//Matcher title = Pattern.compile("<Title> \"(.+?)\"</Title>").matcher(epg); // for old dongle setup
Matcher title=Pattern.compile(“(.+?)”).Matcher(epg);//用于新加密狗设置
//Matcher title=Pattern.compile(“\”(.+?)\”).Matcher(epg);//对于旧的加密狗设置
我有一个字符串形式的xml响应,希望将其解析到matcher对象中。标题将采用以下格式:
<Title> "The Ellen DeGeneres Show"</Title>
<Title> The Ellen DeGeneres Show</Title>
“艾伦·德杰尼勒斯秀”
或此格式:
<Title> "The Ellen DeGeneres Show"</Title>
<Title> The Ellen DeGeneres Show</Title>
艾伦·德杰尼勒斯秀
所以本质上是引号的不同。在选择使用哪种方法之前,如何使用if语句来检查这一点。总结
if(pattern is with quotation marks){
Matcher title = Pattern.compile("<Title> \"(.+?)\"</Title>").matcher(epg);
} else if (pattern is without quotation marks){
Matcher title = Pattern.compile("<Title> (.+?)</Title>").matcher(epg)
}
if(模式带引号){
Matcher title=Pattern.compile(“\”(.+?)\”).Matcher(epg);
}else if(模式不带引号){
Matcher title=Pattern.compile(“(.+?)”).Matcher(epg)
}
我不知道该在if语句中放些什么。你可以试试看
Matcher title = Pattern.compile("<Title>\\s*\"?([^\"]*)\"?</Title>").matcher(epg);
Matcher title=Pattern.compile(“\\s*\”?([^\“]*)\”?”)\”。Matcher(epg);
为了允许在开始标记后有任意数量的空格(\s)。根据@UrosK的建议,我在正则表达式中查找了如何使字符可选。结果发现,我必须在我希望可选的字符后添加一个问号。现在我的声明如下所示:
Matcher title = Pattern.compile("<Title> \"?(.+?)\"?</Title>").matcher(epg);
Matcher title=Pattern.compile(“\”?(.+?)\”?”).Matcher(epg);
尝试为这两种情况编写正则表达式,然后使用
操作符将它们连接起来
以下是我的代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexOptionalQuotationMarks {
public static void main(String[] args) {
String[] input = {
"<Title> \"The Ellen DeGeneres Show\"</Title>"
, "<Title> The Ellen DeGeneres Show</Title>"
};
String regexWithoutQm = "<Title>\\s*\\w[^<]*</Title>";
String regexWithQm = "<Title>\\s*\"[^\"<]*\"\\s*</Title>";
String regexBoth = regexWithoutQm + "|" + regexWithQm;
Pattern p = Pattern.compile(regexBoth);
for (String s : input) {
Matcher m = p.matcher(s);
System.out.format("matching input %s ... %b%n", s, m.find());
}
}
}
import java.util.regex.Matcher;
导入java.util.regex.Pattern;
公共类RegExceptionalQuotationMarks{
公共静态void main(字符串[]args){
字符串[]输入={
““艾伦·德杰尼勒斯秀”
“艾伦·德杰尼勒斯秀”
};
String regexWithoutQm=“\\s*\\w[^尝试使用以下代码:
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
String xml = "<root><Title>test</Title></root>";
Document dDoc = builder.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
XPath xPath = XPathFactory.newInstance().newXPath();
Node node = (Node) xPath.evaluate("//Title", dDoc, XPathConstants.NODE);
System.out.println(node.getTextContent());
final String text = node.getTextContent().trim();
if(text.matches("^\\\".*\\\"$")){
// Between double quotes
}
else{
// No quotes
}
DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=domFactory.newDocumentBuilder();
String xml=“test”;
文档dDoc=builder.parse(新的ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
XPath=XPathFactory.newInstance().newXPath();
Node Node=(Node)xPath.evaluate(“//Title”,dDoc,XPathConstants.Node);
System.out.println(node.getTextContent());
最终字符串text=node.getTextContent().trim();
if(text.matches(“^\\\”*\\\“$”){
//双引号之间
}
否则{
//无报价
}
首先查找“Title”节点,然后检查其内容的模式为什么不在正则表达式中选择引号?-如果是,则不选择needed@UrosK谢谢!我在引号后加上了问号,效果很好!我不熟悉Regex,必须尽快发送新版本(没有时间查看文档)这就是我问stackoverflow.Cool的原因。请随意回答并接受答案。在xml上使用正则表达式通常是不安全的。例如,请使用xml解析器和xpathinstance@MeBigFatGuy它实际上是字符串形式的,我编辑了这个问题,谢谢你指出。这是正确的。+1,这也是乌罗斯的荣誉。:-)这也将匹配un平衡引号,例如,“oops
。还请注意,在开始标记后有一个空格。最后,您可能希望使用[^当然,但根据所描述的用例,可以省去额外的工作。:-)