Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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中的[[Wikipedia:Manual of Style#Links |]]匹配_Java_Xml_Regex_Parsing_Url - Fatal编程技术网

正则表达式与java中的[[Wikipedia:Manual of Style#Links |]]匹配

正则表达式与java中的[[Wikipedia:Manual of Style#Links |]]匹配,java,xml,regex,parsing,url,Java,Xml,Regex,Parsing,Url,我一直在尝试匹配以下字符串- String temp = "[[Wikipedia:Manual of Style#Links|]]" ; 用正则表达式 boolean a = temp.matches("\\[\\[Wikipedia:[a-zA-Z_0-9]*#[a-zA-Z_0-9]*\\|\\]\\]"); "\\[\\[Wikipedia:(.*?)#(.*?)\\|\\]\\]" "\\[\\[Wikipedia:(.*)*#(.+)*\\|\\]\\]" "\\[\\[(.

我一直在尝试匹配以下字符串-

String temp = "[[Wikipedia:Manual of Style#Links|]]" ;
用正则表达式

boolean a = temp.matches("\\[\\[Wikipedia:[a-zA-Z_0-9]*#[a-zA-Z_0-9]*\\|\\]\\]");

"\\[\\[Wikipedia:(.*?)#(.*?)\\|\\]\\]"

"\\[\\[Wikipedia:(.*)*#(.+)*\\|\\]\\]"

"\\[\\[(.*?)#(.*?)\\|\\]\\]"

但它们都没有给出任何正匹配。

只需在自定义角色类中添加一个空格:

String temp = "[[Wikipedia:Manual of Style#Links|]]" ;
temp.matches("\\[\\[Wikipedia:[a-zA-Z_0-9 ]*#[a-zA-Z_0-9]*\\|\\]\\]");  //true

我马上就看到一个问题:您正在使用一个没有空格的字符类来匹配输入和空格

试试这个:

boolean a = temp.matches("\\[\\[Wikipedia:[\\w ]*#[\\w ]+\\|\\]\\]");
请注意,
[a-zA-Z_0-9]
可以替换为
[\w]
(但可以包括所有语言的字母/数字,这应该可以)


可能想试试:看看它是否有助于在更大的文本中嵌入链接?你确定语法正确吗?paren应该在双引号之后和分号之前,第二个正则表达式应该有效。@Jerry是的,你抓住了。编辑。
public static void main(String[] args) {
    String temp = "[[Wikipedia:Manual of Style#Links|]]";
    Pattern pattern = Pattern.compile("\\[\\[Wikipedia:([\\w ]+)#([\\w ]+)\\|\\]\\]");
    Matcher matcher = pattern.matcher(temp);

    if(matcher.find()) {            
        System.out.println("Manual of Style: " + matcher.group(1));
        System.out.println("links : " + matcher.group(2));
    }
}
temp.matches("\\[\\[Wikipedia:([\\w ]+)#([\\w ]+)\\|\\]\\]");