drzaus
drzaus

Reputation: 25004

Regex - Find matching words AND dots that don't begin with prefix

Similar to Regex - Find all matching words that that don't begin with a specific prefix, and may be similar to Regex for matching whole words that contain dots

I need to find all instances of "somehost" that doesn't start with "com."

Specifically, I'm looking to change instances of "somehost" in the address but not the 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" />

Adapting the "(not girl)friend" example in the first linked question, I've tried:

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

Which will match subsite.somehost, othersub.somehost, and .somehost, which also doesn't help at all if I want to just replace the somehost part. Looks like the period (.) is screwing things up...

(In my case, I do have access to lookbehinds)

Upvotes: 1

Views: 128

Answers (1)

anubhava
anubhava

Reputation: 785186

You can use this negative lookbehind:

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

RegEx Demo

i.e. use lookbehind (?<!com\.) just before somehost in your pattern.

Upvotes: 1

Related Questions