如何将所有字符串替换为 Notepad++ 中每个字符串中包含的数字? [英] How to replace all strings to numbers contained in each string in Notepad++?

查看:112
本文介绍了如何将所有字符串替换为 Notepad++ 中每个字符串中包含的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下模式查找所有值:

I'm trying to find all values with following pattern :

value="4"
value="403"
value="200"
value="201"
value="116"
value="15"

并用范围内的值替换它.

and replace it with value inside scopes.

我正在使用以下正则表达式来查找模式:

I'm using the following regex to find the pattern :

.*"\d+"

如何更换?

推荐答案

在 Notepad++ 中进行替换,按 Ctrl+H 打开替换菜单.

In Notepad++ to replace, hit Ctrl+H to open the Replace menu.

然后,如果您选中正则表达式"按钮并希望在替换中使用匹配模式的一部分,则必须使用捕获组"(在 google).例如,假设您要匹配以下每一行

Then if you check the "Regular expression" button and you want in your replacement to use a part of your matching pattern, you must use "capture groups" (read more on google). For example, let's say that you want to match each of the following lines

value="4"
value="403"
value="200"
value="201"
value="116"
value="15"

使用 .*"\d+" 模式并且只想保留数字.然后,您可以在匹配模式中使用捕获组,使用括号 (),如下所示:.*"(\d+)".所以现在在您的替换中,您可以简单地编写 $1,其中 $1 引用第一个捕获组的值,并将返回每个成功匹配的数字.如果您有两个捕获组,例如 (.*)="(\d+)"$1 将返回字符串 value$2 将返回数字.

using the .*"\d+" pattern and want to keep only the number. You can then use a capture group in your matching pattern, using parentheses ( and ), like that: .*"(\d+)". So now in your replacement you can simply write $1, where $1 references to the value of the 1st capturing group and will return the number for each successful match. If you had two capture groups, for example (.*)="(\d+)", $1 will return the string value and $2 will return the number.

所以通过使用:

查找:.*"(\d+)"

替换:$1

它会回报你

4
403
200
201
116
15

请注意,有许多替代和更好的方法可以匹配上述模式.例如,模式 value="([0-9]+)" 会更好,因为它更具体,您将确定它只会匹配这些行.甚至可以在不使用捕获组的情况下进行替换,但这是一个稍微更高级的主题,所以我现在先离开它:)

Please note that there many alternate and better ways of matching the aforementioned pattern. For example the pattern value="([0-9]+)" would be better, since it is more specific and you will be sure that it will match only these lines. It's even possible of making the replacement without the use of capture groups, but this is a slightly more advanced topic, so I'll leave it for now :)

这篇关于如何将所有字符串替换为 Notepad++ 中每个字符串中包含的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆