Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.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/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 如何将单词与regex完全匹配?_Java_Regex_String_String Search - Fatal编程技术网

Java 如何将单词与regex完全匹配?

Java 如何将单词与regex完全匹配?,java,regex,string,string-search,Java,Regex,String,String Search,我可能问错了这个问题,但我想做的是: 给定一个大字符串,它可能有100多行长,精确地匹配和替换一个单词,并确保它不会替换和匹配任何其他字符串的任何部分 例如: Strings to Find = Mac Apple Microsoft Matt Damon I.B.M. Hursley Replacement Strings = MacO AppleO MicrosoftO MattDamonP I.B.M.O HursleyL Input String (with some of th

我可能问错了这个问题,但我想做的是:

给定一个大字符串,它可能有100多行长,精确地匹配和替换一个单词,并确保它不会替换和匹配任何其他字符串的任何部分

例如:

Strings to Find = Mac Apple Microsoft Matt Damon I.B.M. Hursley Replacement Strings = MacO AppleO MicrosoftO MattDamonP I.B.M.O HursleyL Input String (with some of the escape characters included for clarity) = "A file to test if it finds different\r\n bits and bobs like Mac, Apple and Microsoft.\n I.B.M. in Hursley does sum cool stuff!Wow look it's "Matt Damon"\r\n Testing something whichwillerrorMac"\n 要查找的字符串=Mac Apple Microsoft Matt Damon I.B.M.Hursley 替换字符串=MacO AppleO MicrosoftO MattDamonP I.B.M.O HursleyL 输入字符串(为了清晰起见,包含一些转义字符)= “要测试的文件是否找到不同的\r\n 像Mac、苹果和微软这样的小玩意。\n 赫斯利的I.B.M.真是太酷了!哇,看这是“马特·达蒙”\r\n 正在测试将出错的内容MAC“\n 输出

"A file to test if it finds different bits and bobs like MacO, AppleO and MicrosoftO. I.B.M.O in HursleyL do sum cool stuff!Wow look it's "Matt DamonP" Testing something whichwillerrorMac" “要测试的文件是否发现不同 像MacO、AppleO和MicrosoftO这样的小玩意。 HursleyL的I.B.M.O做一些很酷的事情!哇,看这是“马特·达蒙普” 测试会出错的东西“ 我尝试过使用单词边界来使用正则表达式,尽管这在最后一行选择了
“whichwhillerrorMacO”

我还尝试使用
StringTokenizer
类和各种分隔符尝试替换单词,但我尝试替换的一些单词包含这些分隔符


有一个正则表达式可以解决这个问题吗?

\$1O\
替换
\b(Mac | Apple)\b
将不会触及
哪个会出错Mac
-它将匹配
哪个会出错Mac

您希望有多少不同的“要查找的字符串”和“替换字符串”?我想无论哪种方式,我的答案都是一样的,但我想我会检查一下但是我仍然没有解决这个问题,只是简单地为每个单词做了一个相等性检查,尽管这需要很长时间。我不确定这样的问题能不能解决。