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_Wikipedia_Parentheses_Square Bracket - Fatal编程技术网

Java 查找方括号中的文本,但不查找括号中的文本

Java 查找方括号中的文本,但不查找括号中的文本,java,regex,wikipedia,parentheses,square-bracket,Java,Regex,Wikipedia,Parentheses,Square Bracket,如果我有这样一个字符串(来自Wiki标记),我需要用Java解析它: this link (is [[ inParen ]] and) (this) one is [[ notInParen ]] 我希望使用正则表达式来提取[[]]中的文本,但如果它们在括号中,则不这样做。例如,在上面的示例中,它应该返回: notInParen 但忽略: inParen and this 。。。因为它们在括号内。我可以分别找到括号和括号,没有问题: .*\(.*?\).* and .*?\[\[(.*?\

如果我有这样一个字符串(来自Wiki标记),我需要用Java解析它:

this link (is [[ inParen ]] and) (this) one is [[ notInParen ]]
我希望使用正则表达式来提取[[]]中的文本,但如果它们在括号中,则不这样做。例如,在上面的示例中,它应该返回:

notInParen
但忽略:

inParen and this
。。。因为它们在括号内。我可以分别找到括号和括号,没有问题:

.*\(.*?\).* and .*?\[\[(.*?\]\].*

…但不知道如何找到[],查找括号,然后忽略。谢谢

是否需要一次性完成?你可以做:

  • 解析字符串并删除括号中包含的所有子字符串
  • 再次解析结果并使用
    [[
    ]
    获取所有所需的维基百科链接
这解决了问题,使问题更容易解决

在步骤1之后,您有:
这个链接是[[notInParen]]


第2步之后,您有:
notInParen

是否需要一次性完成?你可以做:

  • 解析字符串并删除括号中包含的所有子字符串
  • 再次解析结果并使用
    [[
    ]
    获取所有所需的维基百科链接
这解决了问题,使问题更容易解决

在步骤1之后,您有:
这个链接是[[notInParen]]

在步骤2之后,您有:
notInParen

这是一个很好的正则表达式

\(.*?\)|\[\[(.*?)]]
您想要的比赛将在第1组

仅供参考,为了使其更好地执行,您可以通过使用否定的字符类替换惰性匹配来最小化回溯

在Java中,这变成了

String ResultString = null;
try {
    Pattern regex = Pattern.compile("\\(.*?\\)|\\[\\[(.*?)\\]\\]", Pattern.DOTALL | Pattern.MULTILINE);
    Matcher regexMatcher = regex.matcher(subjectString);
    if (regexMatcher.find()) {
        ResultString = regexMatcher.group(1);
    } 
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}
请注意,对于替换的第一部分不匹配的情况,组1将为空。

这是一个很好的正则表达式

\(.*?\)|\[\[(.*?)]]
您想要的比赛将在第1组

仅供参考,为了使其更好地执行,您可以通过使用否定的字符类替换惰性匹配来最小化回溯

在Java中,这变成了

String ResultString = null;
try {
    Pattern regex = Pattern.compile("\\(.*?\\)|\\[\\[(.*?)\\]\\]", Pattern.DOTALL | Pattern.MULTILINE);
    Matcher regexMatcher = regex.matcher(subjectString);
    if (regexMatcher.find()) {
        ResultString = regexMatcher.group(1);
    } 
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

请注意,对于替换的第一部分不匹配的情况,组1将为空。

您也可以这样做

String data = "this link (is [[ inParen ]] and) (this) one is [[ notInParen ]]" +
        " this link (is [[ inParen ]] and) (this) one is [[ notInParen ]]";

boolean insideParentheses = false;
int start = 0, end = 0;
for (int i = 0; i < data.length() - 1; i++) {
    if (data.charAt(i) == '(')
        insideParentheses = true;
    if (data.charAt(i) == ')')
        insideParentheses = false;
    // -> [[ and ]] inside Parentheses are not important
    if (!insideParentheses && 
            data.charAt(i) == '[' && data.charAt(i + 1) == '[') {
        start = i;
    }
    if (!insideParentheses && 
            data.charAt(i) == ']' && data.charAt(i + 1) == ']') {
        end = i;
        System.out.println(data.substring(start, end + 2));
    }
}

你也可以这样做

String data = "this link (is [[ inParen ]] and) (this) one is [[ notInParen ]]" +
        " this link (is [[ inParen ]] and) (this) one is [[ notInParen ]]";

boolean insideParentheses = false;
int start = 0, end = 0;
for (int i = 0; i < data.length() - 1; i++) {
    if (data.charAt(i) == '(')
        insideParentheses = true;
    if (data.charAt(i) == ')')
        insideParentheses = false;
    // -> [[ and ]] inside Parentheses are not important
    if (!insideParentheses && 
            data.charAt(i) == '[' && data.charAt(i + 1) == '[') {
        start = i;
    }
    if (!insideParentheses && 
            data.charAt(i) == ']' && data.charAt(i + 1) == ']') {
        end = i;
        System.out.println(data.substring(start, end + 2));
    }
}
啊,太棒了!其中一个“砰的一头撞在墙上”,简单的回答就盯着我看。不过,我很想看看正则表达式的版本!啊,太棒了!其中一个“砰的一头撞在墙上”,简单的回答就盯着我看。不过,我很想看看正则表达式的版本@JeffThompson将if(regexMatcher.find())更改为
while(regexMatcher.find())
并忽略
null
s@JeffThompson将
if(regexMatcher.find())
更改为
while(regexMatcher.find())
并忽略
null
s