Search/Replace (in an XML-File) the value of one a

2019-08-31 01:49发布

问题:

My XML File (~6000 lines) contains lines like these:

<sms protocol="0" address="+12341234" date="12341234" type="2" subject="null" body="smstext" toa="0" sc_toa="0" service_center="+12341234" read="1" status="-1" locked="0" date_sent="null" readable_date="Jan 6, 1980 1:02:14 AM" contact_name="Patrick" />

and i want to replace the text of contact_name with something else, but only if type="2".

I mean it would be very easy if i could bookmark all the type="2" lines and then search/replace on the bookmarked lines only, but i haven't found such an option.

So how would i do that in Notepad++?

EDIT: changed the title, i meant attribute not tag ...

回答1:

You can try following:

Find: (?<=type="2")(.*?)(contact_name=")(.*?)(")

Replace with: \1\2SomeOtherContactName\4

(?<=type="2") let's you see if string contains type="2", but not capturing string

(.*?) is a first group between type and contact name (any symbol)

(contact_name=") - is a second group

(.*?) group #3 wicth you want to replace with some other value (any symbol, or can look like [A-Za-z]

(") - just contains closing quote. And we use it as group 4 in replace statement



标签: notepad++