简体   繁体   English

Java:此正则表达式有什么问题?

[英]Java:What is wrong with this regex?

I am trying to get the text inside a tag ie <text> . 我正在尝试将文本包含在<text>标记中。 I am doing: 我在做:

Pattern pattern = Pattern.compile("(?<=\\<).*(?=\\>)");

I think that this says: any character 0 or more times that before is a < (positive lookbehind) and followed by > (positive lookahead). 我认为这是这样说的:任何比0或更多倍的字符之前是< (正向后看),然后是> (正向前看)。

Matcher m = pattern.matcher(data);  
if (!m.matches()) continue; //Called in a for loop  

But there is no match for eg the input <text> some other stuff here . 但是这里没有匹配输入<text> some other stuff here

What am I doing wrong here? 我在这里做错了什么?

When you are using matches() , the entire input string must match the expression. 当使用matches() ,整个输入字符串必须与表达式匹配。 If you want to find substrings, you may use find() instead. 如果要查找子字符串,则可以改用find()

Don't use m.matches() but m.find() . 不要使用m.matches()m.find()使用m.find()

From the JavaDoc on matches() : Match matches()JavaDoc matches()

Attempts to match the entire region against the pattern. 尝试根据图案匹配整个区域。

You can try this to match: 您可以尝试以下匹配:

public static void main(String[] args) {
    String input = "<text> Some Value </text> <a>  <testTag>";
    Pattern p = Pattern.compile("<(\\w.*?)>");
    Matcher m = p.matcher(input);

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

I don't quite understand your regular expression, but this works for me: 我不太了解您的正则表达式,但这对我有用:

String text = "<text>";
Pattern p = Pattern.compile(".*<(.*)>.*");
Matcher m = p.matcher(text);
System.out.println(m.matches());
System.out.println(m.group(1));

this displays: 显示:

true
text

Is that what you need? 那是你需要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM