简体   繁体   English

如何将所有字符串替换为Notepad ++中每个字符串中包含的数字?

[英]How to replace all strings to numbers contained in each string in 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+"

How can I do a replacement? 我怎样才能更换?

In Notepad++ to replace, hit Ctrl + H to open the Replace menu. 在要替换的Notepad ++中, 按Ctrl + H打开“替换”菜单。

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"

using the .*"\\d+" pattern and want to keep only the number. 使用.*"\\d+"模式并且只想保留数字。 You can then use a capture group in your matching pattern, using parentheses ( and ) , like that: .*"(\\d+)" . 然后,您可以在匹配模式中使用捕获组,使用括号() ,如下所示: .*"(\\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. 因此,现在在您的替代品中,您可以简单地写入$1 ,其中$ 1引用第一个捕获组的值,并将返回每个成功匹配的数字。 If you had two capture groups, for example (.*)="(\\d+)" , $1 will return the string value and $2 will return the number. 如果你有两个捕获组,例如(.*)="(\\d+)"$1将返回字符串value$2将返回该数字。

So by using: 所以通过使用:

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

Replace: $1 替换: $1

It will return you 它会回报你

4
403
200
201
116
15

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. 例如,模式value="([0-9]+)"会更好,因为它更具体,你将确保它只匹配这些行。 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 :) 甚至可以在不使用捕获组的情况下进行替换,但这是一个稍微高级的主题,所以我现在就把它留下:)

psxls gave a great answer but I think my Notepad++ version is slightly different so the $ (dollar sign) capturing did not work. psxls给出了一个很好的答案,但我认为我的Notepad ++版本略有不同,所以$(美元符号)捕获不起作用。

I have Notepad++ v.5.9.3 and here's how you can accomplish your task: 我有Notepad ++ v.5.9.3 ,以下是如何完成任务的:

Search for the pattern: value=\\"([0-9]*)\\" And replace with: \\1 (whatever you want to do around that capturing group) 搜索模式: value = \\“([0-9] *)\\”并替换为: \\ 1 (无论你想在该捕获组周围做什么)

Ex. 防爆。 Surround with square brackets 用方括号环绕

[\\1] --> will produce value="[4]" [\\ 1] - > 会产生值=“[4]”

Replace (.*")\\d+(") 替换(.*")\\d+(")

With $1x$2 $1x$2

Where x is your "value inside scopes". 其中x是你的“范围内的价值”。

I have Notepad++ v6.8.8 我有Notepad ++ v6.8.8

Find : [([a-zA-Z])] 发现 :[([a-zA-Z])]

Replace : [\\'\\1\\'] 替换 :[\\'\\ 1 \\']

Will produce: $array[XYZ] => $array['XYZ'] 将产生:$ array [XYZ] => $ array ['XYZ']

Find: value="([\\d]+|[\\d])" 查找: value="([\\d]+|[\\d])"

Replace: \\1 替换: \\1

It will really return you 它会真的回报你

4
403
200
201
116
15

js: JS:

a='value="4"\nvalue="403"\nvalue="200"\nvalue="201"\nvalue="116"\nvalue="15"';
a = a.replace(/value="([\d]+|[\d])"/g, '$1');
console.log(a);

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

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