正则表达式 - 查找不以前缀开头的匹配单词和点

时间:2016-01-13 16:20:21

标签: regex

Regex - Find all matching words that that don't begin with a specific prefix类似,可能类似于Regex for matching whole words that contain dots

我需要查找不以“somehost”开头的所有“com.”实例

具体来说,我希望更改address中的“somehost”实例,而不是contract

<endpoint address="https://subsite.somehost.com/someservice.svc" ... 
    contract="com.somehost.subsite.someservice.MyServiceSoap" />
<endpoint address="https://othersub.somehost.com/anotherservice.svc"  ...
    contract="com.somehost.othersub.anotherservice.MyOtherServiceSoap" />

调整first linked question中的“(非女孩)朋友”示例,我尝试过:

\b(?!com\.)[\w\.]*somehost\b

哪个匹配subsite.somehostothersub.somehost.somehost,如果我只想替换somehost部分,它们根本没用。看起来期间(.)搞砸了......

(在我的情况下,我确实可以访问lookbehinds)

1 个答案:

答案 0 :(得分:1)

你可以使用这种负面的背后隐藏:

[\w-]+\.(?<!com\.)somehost(?:\.[\w-]+)+

RegEx Demo

即。在模式中(?<!com\.)之前使用lookbehind somehost