如何用正则表达式精确匹配单词?
我可能会错误地问这个问题,但我想做的是:
给定一个大字符串,它可能有数百行长匹配并精确替换一个单词,并确保它不会替换和匹配任何其他单词的任何部分细绳。
例如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
\b(Mac|Apple)\b
替换为\$1O\
不会影响whichwillerrorMac
- 它将匹配whichwill-Mac不过。
Replacing
\b(Mac|Apple)\b
with\$1O\
will not touchwhichwillerrorMac
- it will matchwhichwill-Mac
though.