Reputation: 2217
I have the following string:
name:ABC;
username:XYZ;
I want to remove the name value ABC
from the string, so I am using the following regex:
((?<=name))(.*?)(?=;)
It successfully selects the name
value, but the problem is it also selects the username
value.
I want to remove only the name
values not username
values.
How can I do that?
SOLUTION:
**Both Avinash
and Bohemian
answer's are correct for this question.
By Avinash
answer we can exclude all the words contains name
as substring and with Bohemian
answer we can exclude specific words.
Upvotes: 1
Views: 36
Reputation: 425043
Modifying your regex, you can combine two look behinds, one positive, one negative:
(?<!username)(?<=name).*?(?=;)
Note also the removal of unnecessary brackets.
Avinash's answer is probably what I would use in this case, but I thought I would show this way in case \b
didn't work for future visitors, eg all data whose name ends in name
(streetname
, cityname
, etc) except username
.
Upvotes: 1
Reputation: 174706
Use word boundary \b
before name
.
(?<=\bname:)(.*?)(?=;)
or
(?<=\bname:)[^;]*
Upvotes: 2