Neil
Neil

Reputation: 794

Regex to remove a whole phrase from the match

I am trying to remove a whole phrase from my regex(PCRE) matches

if given the following strings

test:test2:test3:test4:test5:1.0.department
test:test2:test3:test4:test5:1.0.foo.0.bar
user.0.display
"test:test2:test3:test4:test5:1.0".division

I want to write regex that will return:

.department
.foo.0.bar
user.0.display
.division

Now I thought a good way to do this would be to match everything and then remove test:test2:test3:test4:test5:1.0 and "test:test2:test3:test4:test5:1.0" but I am struggling to do this

I tried the following

\b(?!(test:test2:test3:test4:test5:1\.0)|("test:test2:test3:test4:test5:1\.0"))\b.*

but this seems to just remove the first tests from each and thats all. Could anyone help on where I am going wrong or a better approach maybe?

Upvotes: 1

Views: 49

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

I suggest searching for the following pattern:

"?test:test2:test3:test4:test5:1\.0"?

and replacing with an empty string. See the regex demo and the regex graph:

enter image description here

The quotation marks on both ends are made optional with a ? (1 or 0 times) quantifier.

Upvotes: 1

Related Questions