简体   繁体   中英

Notepad++ Regex how do I replace characters but only when between double quotes

I'm looking to replace all the underscore ( _ ) characters between the first pair of double quotes ( " ) with full stops ( . ) in this .xml

I've used this regex android:name="([A-Za-z0-9_.]+)" and got the selection android:name="com_android_contacts" , but how do I change the _ into . within this selection?

<package-redirections android:name="com_android_contacts" android:resource="@xml/com_android_contacts" android:minSdkVersion="16" />

to

<package-redirections android:name="com.android.contacts" android:resource="@xml/com_android_contacts" android:minSdkVersion="16" />

Thank you.

You can use the regex:

(?:android:name="[^"_]*)\K_([^"_]*)

And replace with .$1 as many times as necessary until there is no more replacements.

Make sure you have opted for the regular expression search. I'm not sure about version differences, but this works on v6.1.8.

This works for any number of dots in the android.name attribute.

\\K resets the match so that you don't have to put back android:name


Btw: In PCRE flavoured regex, you can use this:

(?:android:name="[^"_]*|\G)\K_([^"_]*)

Which replaces in a single replace all the underscores to dots.

\\G matches at the end of the previous match.

I am not sure if you can replace at once in notepad++, but if you say if has only 2 _ then you can use this: find

(android:name="[^"\._]*)(_)([^"\._]*)(_)([^"\._]*?")

replace to:

\1\.\3\.\5

If all lines look like this (ie three elements with _ as delimiter) you could try Search:

(android:name="[^_]+)_([^_]+)_([^"]+")

Replace:

$1.$2.$3"

Explanation:

The values in brackets () are saved in variables calles $1, $2 and $3 (from left to right) [^_] = all characters that are not _

In your example $1 = android:name="com The following _ is to be replaced by a . etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM