Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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,如何编写模式(Java)来匹配除给定单词列表之外的任何字符序列 我需要找出一个给定的代码是否有任何文本被标签包围,比如除了一个给定的单词列表。 例如,我想检查除了标记所包围的“一”和“二”之外,是否还有其他单词 "This is the first tag <span>one</span> and this is the third <span>three</span>" “这是第一个标记,这是第三个标记” 模式应该与上面的字符串匹配,因为单词

如何编写模式(Java)来匹配除给定单词列表之外的任何字符序列

我需要找出一个给定的代码是否有任何文本被标签包围,比如除了一个给定的单词列表。 例如,我想检查除了标记所包围的“一”和“二”之外,是否还有其他单词

"This is the first tag <span>one</span> and this is the third <span>three</span>"
“这是第一个标记,这是第三个标记”
模式应该与上面的字符串匹配,因为单词“三”被标记包围,并且不是给定单词列表(“一”、“二”)的一部分。

使用以下方法:

if (!Pattern.matches(".*(word1|word2|word3).*", "word1")) {
    System.out.println("We're good.");
};

您正在检查模式是否与字符串不匹配。

向前看可以执行以下操作:

\b(?!your|given|list|of|exclusions)\w+\b
火柴

  • 单词边界(单词的开头)
  • 后面没有任何“您的”、“给定的”、“列表”、“排除项”
  • 后跟多个单词字符
  • 后跟单词边界(单词末尾)

实际上,它匹配任何未排除的单词。

这应该可以让您开始学习

import java.util.regex.*;

// >(?!one<|two<)(\w+)/
// 
// Match the character “>” literally «>»
// Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!one|two)»
//    Match either the regular expression below (attempting the next alternative only if this one fails) «one»
//       Match the characters “one<” literally «one»
//    Or match regular expression number 2 below (the entire group fails if this one fails to match) «two»
//       Match the characters “two<” literally «two»
// Match the regular expression below and capture its match into backreference number 1 «(\w+)»
//    Match a single character that is a “word character” (letters, digits, etc.) «\w+»
//       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Match the characters “/” literally «</»
List<String> matchList = new ArrayList<String>();
try {
    Pattern regex = Pattern.compile(">(?!one<|two<)(\\w+)/");
    Matcher regexMatcher = regex.matcher(subjectString);
    while (regexMatcher.find()) {
        matchList.add(regexMatcher.group(1));
    } 
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}
import java.util.regex.*;
//>(?!一»
//断言不可能从这个位置开始匹配下面的正则表达式(负前瞻)«(?1 | 2)»
//匹配下面的正则表达式(仅当此正则表达式失败时才尝试下一个正则表达式)«一»

//匹配字符“Onehanks for you response,但这不起作用。我在问题描述中添加了更多信息。您是否意识到您现在的问题与编辑之前的问题之间存在着本质的区别,以至于在编辑之前回答问题的人基本上浪费了他们的时间?请尝试从下次开始就绝对清楚。否这里的一个有水晶球或者可以读懂你的心思。我想你可能想把图案中的“一”和“二”改为“一”