简体   繁体   English

Notepad ++正则表达式如何替换字符,但仅在双引号之间

[英]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 我正在寻找在此.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 . 我使用了此正则表达式android:name="([A-Za-z0-9_.]+)"并选择了android:name="com_android_contacts" ,但是如何将_更改为. 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. 并根据需要用.$1替换多次,直到没有更多替换为止。

Make sure you have opted for the regular expression search. 确保选择正则表达式搜索。 I'm not sure about version differences, but this works on v6.1.8. 我不确定版本差异,但这适用于v6.1.8。

This works for any number of dots in the android.name attribute. 这适用于android.name属性中的任意数量的点。

\\K resets the match so that you don't have to put back android:name \\K重置匹配项,因此您不必放回android:name


Btw: In PCRE flavoured regex, you can use this: 顺便说一句:在PCRE风格的正则表达式中,您可以使用以下代码:

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

Which replaces in a single replace all the underscores to dots. 用单个替换将所有下划线替换为点。

\\G matches at the end of the previous match. \\G在上一场比赛的末尾进行比赛。

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 我不确定是否可以在notepad ++中立即替换,但是如果您说如果只有2 _,则可以使用此命令: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 _ 方括号()中的值保存在变量$ 1,$ 2和$ 3中(从左到右)[^ _] =所有非_的字符

In your example $1 = android:name="com The following _ is to be replaced by a . etc. 在您的示例中$ 1 = android:name =“ com以下_将替换为。等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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