如何使用java正则表达式语法(不在java代码中)查找前面没有100个字符的单词
首先,我必须使用Java正则表达式语法(不在Java代码内部)。我需要找到一个单词let say如何使用java正则表达式语法(不在java代码中)查找前面没有100个字符的单词,java,regex,Java,Regex,首先,我必须使用Java正则表达式语法(不在Java代码内部)。我需要找到一个单词let saywarning,只要前面没有100个字符的单词let say请参见。它可以是警告的变体。例如,警告,后面必须有空格字符、标点符号或字母,而不是任何其他字符,如)、/,或任何类似的字符。这就是我到目前为止所做的: (?i)[^s&&e&&e].{0,100}(warning)/w* 这里有两个逻辑非常相似的例子。唯一改变的是我们如何解析单词是否“后面有空格字符、标点符号
warning
,只要前面没有100个字符的单词let say请参见
。它可以是警告的变体。例如,警告
,后面必须有空格字符、标点符号或字母,而不是任何其他字符,如)
、/
,或任何类似的字符。这就是我到目前为止所做的:
(?i)[^s&&e&&e].{0,100}(warning)/w*
这里有两个逻辑非常相似的例子。唯一改变的是我们如何解析单词是否“后面有空格字符、标点符号或字母,而不是任何其他字符,如
)
、/
,或类似的字符”。我们可以排除我们不需要的字符()
、/
等),也可以要求我们需要的字符(空格、标点符号等)
原件: 此示例使用负回溯来确保
警告
前面没有请参见
。它还使用单词边界来确定see
和warning[a-z]*
是整个单词,还是仅仅是单词的一部分。最后,我们有一个消极的前瞻,以确保警告[a-z]*
后面没有我们不想要的字符类[)/]
(?<! (?# start negative lookbehind)
\bsee\b (?# the word "see" surrounded by word boundaries)
) (?# end negative lookbehind)
\s+ (?# 1+ whitespace characters separating words)
\b (?# word boundary)
( (?# start capture group)
warning (?# the word "warning")
[a-z]* (?# with optional additional characters)
) (?# end capture group)
(?! (?# start negative lookahead)
[)/] (?# character class of unwanted characters)
) (?# end negative lookahead)
\b (?# word boundary)
缩小:(
演示:因此您正在寻找类似someString.substring(0100).contains(“warning”)
?(但对于整个字符串,而不仅仅是前100个字符)的regex等价物
(?<! (?# start negative lookbehind)
\bsee\b (?# the word "see" surrounded by word boundaries)
) (?# end negative lookbehind)
\s+ (?# 1+ whitespace characters separating words)
\b (?# word boundary)
( (?# start capture group)
warning (?# the word "warning")
[a-z]* (?# with optional additional characters)
) (?# end capture group)
(?= (?# start lookahead)
[\s.,] (?# character class of allowed characters)
| (?# OR)
$ (?# the end of string)
) (?# end negative lookahead)