如何用正则表达式精确匹配单词?

发布于 2024-08-14 08:24:03 字数 924 浏览 9 评论 0原文

我可能会错误地问这个问题,但我想做的是:

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

例如:

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  

输出

"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"  

我尝试使用正则表达式使用字边界,尽管这会在最后一行找到 'whichwhillerrorMacO'

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

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

I might be asking this question incorrectly but what I would like to do is the following:

Given a large String which could be many 100s of lines long match and replace a word exactly and make sure it does not replace and match any part of any other String.

For example :

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  

OUTPUT

"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"  

I have tried using Regex using word boundaries, although this picks up 'whichwhillerrorMacO' on the last line.

I have also tried using the StringTokenizer class and various delimiters to try and replace words, but some of the words I am trying to replace contains these delimiters.

Is there a regex that would solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里梦着梦中梦 2024-08-21 08:24:03

\b(Mac|Apple)\b 替换为 \$1O\ 不会影响 whichwillerrorMac - 它将匹配 whichwill-Mac不过。

Replacing \b(Mac|Apple)\b with \$1O\ will not touch whichwillerrorMac - it will match whichwill-Mac though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文