Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java正则表达式提取标记之间的文本_Java_Regex - Fatal编程技术网

Java正则表达式提取标记之间的文本

Java正则表达式提取标记之间的文本,java,regex,Java,Regex,我有一个带有一些自定义标记的文件,我想编写一个正则表达式来提取标记之间的字符串。例如,如果我的标记是: [customtag]我要提取的字符串[/customtag] 如何编写正则表达式来仅提取标记之间的字符串。此代码似乎是朝着正确方向迈出的一步: Pattern p = Pattern.compile("[customtag](.+?)[/customtag]"); Matcher m = p.matcher("[customtag]String I want to extract[/cus

我有一个带有一些自定义标记的文件,我想编写一个正则表达式来提取标记之间的字符串。例如,如果我的标记是:

[customtag]我要提取的字符串[/customtag]
如何编写正则表达式来仅提取标记之间的字符串。此代码似乎是朝着正确方向迈出的一步:

Pattern p = Pattern.compile("[customtag](.+?)[/customtag]");
Matcher m = p.matcher("[customtag]String I want to extract[/customtag]");

不知道下一步该怎么办。有什么想法吗?谢谢。

你说得对。现在您只需要提取所需的组,如下所示:

final Pattern pattern = Pattern.compile("<tag>(.+?)</tag>", Pattern.DOTALL);
final Matcher matcher = pattern.matcher("<tag>String I want to extract</tag>");
matcher.find();
System.out.println(matcher.group(1)); // Prints String I want to extract
final Pattern=Pattern.compile((.+?)”,Pattern.DOTALL);
final Matcher Matcher=pattern.Matcher(“我想提取的字符串”);
matcher.find();
System.out.println(matcher.group(1));//打印我要提取的字符串
如果要提取多个点击,请尝试以下操作:

public static void main(String[] args) {
    final String str = "<tag>apple</tag><b>hello</b><tag>orange</tag><tag>pear</tag>";
    System.out.println(Arrays.toString(getTagValues(str).toArray())); // Prints [apple, orange, pear]
}

private static final Pattern TAG_REGEX = Pattern.compile("<tag>(.+?)</tag>", Pattern.DOTALL);

private static List<String> getTagValues(final String str) {
    final List<String> tagValues = new ArrayList<String>();
    final Matcher matcher = TAG_REGEX.matcher(str);
    while (matcher.find()) {
        tagValues.add(matcher.group(1));
    }
    return tagValues;
}
publicstaticvoidmain(字符串[]args){
最后一个字符串str=“applehelloorangepear”;
System.out.println(Arrays.toString(getTagValues(str.toArray());//Prints[苹果、橘子、梨]
}
私有静态最终模式标记_REGEX=Pattern.compile((.+?)”,Pattern.DOTALL);
私有静态列表getTagValues(最终字符串str){
最终列表tagValues=new ArrayList();
最终匹配器匹配器=TAG_REGEX.Matcher(str);
while(matcher.find()){
tagValues.add(matcher.group(1));
}
返回标记值;
}
然而,我同意正则表达式并不是最好的答案。我会使用XPath查找我感兴趣的元素。有关更多信息,请参阅。

我在回答之前加上“不应该使用正则表达式来解析XML——这只会导致边缘情况无法正常工作,并且在尝试修复时,正则表达式的复杂性会永远增加。”

也就是说,您需要匹配字符串并抓取所需的组:

if (m.matches())
{
   String result = m.group(1);
   // do something with result
}

老实说,正则表达式不是这种类型解析的最佳方法。您发布的正则表达式对于简单的情况可能非常有效,但是如果事情变得更加复杂,您将遇到巨大的问题(这也是您无法用正则表达式可靠地解析HTML的原因)。我知道你可能不想听这个,我知道当我问同样类型的问题时我不想听,但是在我停止尝试使用正则表达式处理所有事情之后,字符串解析对我来说变得更加可靠

是一个非常棒的标记器,它使手工编写解析器变得非常容易(我强烈建议使用jtopas而不是标准的java scanner/etc.库)。若你们想看到jtopas的实际应用,我写的一些解析器是否使用jtopas来解析文件类型


如果要解析XML文件,则应使用XML解析器库。除非你只是为了好玩,否则不要自己动手。有很多经验证的选项

一种通用的、更简单的、有点原始的方法来查找标记、属性和值

    final Pattern pattern = Pattern.compile("tag\\](.+?)\\[/tag");
    final Matcher matcher = pattern.matcher("[tag]String I want to extract[/tag]");
    matcher.find();
    System.out.println(matcher.group(1));
    Pattern pattern = Pattern.compile("<(\\w+)( +.+)*>((.*))</\\1>");
    System.out.println(pattern.matcher("<asd> TEST</asd>").find());
    System.out.println(pattern.matcher("<asd TEST</asd>").find());
    System.out.println(pattern.matcher("<asd attr='3'> TEST</asd>").find());
    System.out.println(pattern.matcher("<asd> <x>TEST<x>asd>").find());
    System.out.println("-------");
    Matcher matcher = pattern.matcher("<as x> TEST</as>");
    if (matcher.find()) {
        for (int i = 0; i <= matcher.groupCount(); i++) {
            System.out.println(i + ":" + matcher.group(i));
        }
    }
Pattern=Pattern.compile((.*)”;
System.out.println(pattern.matcher(“TEST”).find());
System.out.println(pattern.matcher(“”.find());
System.out.println(“----”);
Matcher Matcher=pattern.Matcher(“测试”);
if(matcher.find()){
对于(int i=0;i请尝试以下方法:

Pattern p = Pattern.compile(?<=\\<(any_tag)\\>)(\\s*.*\\s*)(?=\\<\\/(any_tag)\\>);
Matcher m = p.matcher(anyString);
Pattern p=Pattern.compile(?
String s=“test1”;
字符串模式=“\\([^\\]+)\\”;
整数计数=0;
Pattern p=Pattern.compile(Pattern);
匹配器m=匹配器p;
while(m.find())
{
系统输出println(m.group(2));
计数++;
}

对于初学者,您需要避开
[]
方括号,方括号是正则表达式中的元字符。非常感谢,这正是我所需要的。我将研究XPath,但现在我认为这个解决方案会起作用。我的应用程序非常简单,可能会保持这种方式。再次感谢!这个字符串
“applehelloorangepear”
?如果没有close标记,我们如何获得
pear
?概括:私有字符串extractDataFromTags(字符串标记){Pattern Pattern=Pattern.compile((.+?)”);Matcher-Matcher=Pattern.Matcher(tag);Matcher.find();return(Matcher.group(1));//打印我要提取的字符串或引发异常}谢谢你的建议。我已经为它们添加了书签,我肯定会在未来的项目中考虑使用它。现在我可能会使用regex方法,因为我正在解析的文件非常小/简单。如果有一系列不同的标记或嵌套标记,如
Macloves-it
macloved,那么模式会是什么您的答案
?请编辑iString str = "<TR> <TD>1Q Ene</TD> <TD>3.08%</TD> </TR>"; Pattern p = Pattern.compile("(?<=\\<TD\\>)(\\s*.*\\s*)(?=\\<\\/TD\\>)"); Matcher m = p.matcher(str); while(m.find()){ Log.e("Regex"," Regex result: " + m.group()) }
    String s = "<B><G>Test</G></B><C>Test1</C>";

    String pattern ="\\<(.+)\\>([^\\<\\>]+)\\<\\/\\1\\>";

       int count = 0;

        Pattern p = Pattern.compile(pattern);
        Matcher m =  p.matcher(s);
        while(m.find())
        {
            System.out.println(m.group(2));
            count++;
        }