java - 正则表达式用于提取标签之间的文本而不是标签

标签 java html regex tags nodes

我有以下文本:

<Data>
    <xpath>/Temporary/EIC/SpouseSSNDisqualification</xpath>
    <Gist>AllConditionsTrue</Gist>
    <Template>
        <Text id="1">Your spouse is required to have a Social Security number instead of an ITIN to claim this credit.  This is based on the IRS rules for claiming the Earned Income Credit.</Text>
    </Template>
</Data>
<Data>
    <xpath>/Temporary/EIC/SpouseSSNDisqualification</xpath>
    <Gist>AllConditionsTrue</Gist>
    <Template>
        <Text id="1">Your spouse has the required Social Security number instead of an ITIN to claim this credit.  This is based on the IRS rules for claiming the Earned Income Credit.</Text>
    </Template>
</Data>

我想提取 xpath 之间的数据标签而不是标签本身。

输出应该是:

/Temporary/EIC/SpouseSSNDisqualification

/Temporary/EIC/SpouseSSNDisqualification

这个正则表达式似乎给了我所有的文本,包括 xpath我不想要的标签:

<NodeID>(.+?)<\/NodeID>

编辑:

这是我的 Java 代码,但我不确定这是否会增加我的问题的值(value):

    try {
        String xml = FileUtils.readFileToString(file);
        Pattern p = Pattern.compile("<xpath>(.+?)<\\/xpath>");
        Matcher m = p.matcher(xml);

        while(m.find()) {
            System.out.println(m.group(0));
        }
    }

最佳答案

简单。这是因为您获取了所有结果,而不仅仅是第 1 组的值。

String nodestr = "<xpath>/Temporary/EIC/SpouseSSNDisqualification</xpath>";
String regex = "<xpath>(.+?)<\/xpath>";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(nodestr);
if (matcher.matches()) {
    String tag_value = matcher.group(1); //taking only group 1
    System.out.println(tag_value); //printing only group 1
}

关于java - 正则表达式用于提取标签之间的文本而不是标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36803854/

相关文章:

javascript - 排除特定单词及其后面的任何内容

java - Java 中 Thread 的自定义实现 : Is it possible though JNI?

java - Toast 中没有文字

PHP/MySQL/TextArea 问题

c# - 用于检查输入字符串的正则表达式只是波斯语

javascript - 如何使用 .match javascript 方法来搜索数组和字符串?

java - 如何创建对象的 JSONObject?安卓

java - 添加一些图片到 BufferdImage 然后保存

javascript - Bootstrap 复选框中的 onchange 事件

html - 如何只在悬停时显示?