Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.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正则表达式,直到某些单词/文本/字符 请考虑以下内容: That is, it matches at any position that has a non-word character to the left of it, and a word character to the right of it._Java_Regex - Fatal编程技术网

java正则表达式,直到某些单词/文本/字符 请考虑以下内容: That is, it matches at any position that has a non-word character to the left of it, and a word character to the right of it.

java正则表达式,直到某些单词/文本/字符 请考虑以下内容: That is, it matches at any position that has a non-word character to the left of it, and a word character to the right of it.,java,regex,Java,Regex,如何获得以下结果: That is, it matches at any position that has a non-word character to the 这就是在离开之前的一切 String result = inputString.replace("(.*?)left.*", "$1"); ^锚定到字符串的开头 *?尽可能少地匹配任何字符 \b匹配单词边界 left匹配字符串文字“left” *匹配字符串的其余部分 $将锚定到字符串的末尾 $1在() 如果您想使用任何单词

如何获得以下结果:

That is, it matches at any position that has a non-word character to the 
这就是在
离开之前的一切

String result = inputString.replace("(.*?)left.*", "$1");
  • ^
    锚定到字符串的开头
  • *?
    尽可能少地匹配任何字符
  • \b
    匹配单词边界
  • left
    匹配字符串文字
    “left”
  • *
    匹配字符串的其余部分
  • $
    将锚定到字符串的末尾
  • $1
    ()

如果您想使用任何单词(不仅仅是
“left”
),请小心避开它。您可以使用
Pattern.quote(word)
来转义字符串。

答案实际上是
/(.*)\Wleft\w/
,但它与字符串中的任何内容都不匹配

That is, it matches at any position that has a non-word character to the left of it, and a word character to the right of it.

我很抱歉,唯一可能出现的缺陷是如果有你没有的新行mention@London,它不应该起作用。将字符串和正则表达式中的left替换为“-left”。或者“left”不是可变文本?那么,是什么起了作用呢?@sin在批评它之前,请阅读完整的答案。我解释了如何在末尾使用变量词。@如果是这种情况,则必须使用不同的边界匹配(例如,
?是的,英语是一件有趣的事情,有些人会说一个单词是一个字符类,其他人会说它不是左的而不是左的。我,我一点也不重视,从来没有。这些薄弱的问题怎么会不经过过滤?至少应该做的是一个具体的真实例子,至少有人不知道“单词”是什么不要感到困惑。@sin这些弱的评论怎么会不被过滤?至少应该解释一下这种困惑。这个问题的问题是使用一个示例文本,它本身可能是问题文本的一部分。好像世界上没有其他句子。我想它应该是一个引导词boundry是什么。
That is, it matches at any position that has a non-word character to the left of it, and a word character to the right of it.