112

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?

Ooker
  • 1,012
  • 2
  • 15
  • 37
Anton Selin
  • 2,832
  • 5
  • 16
  • 23

5 Answers5

194

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

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. 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.

So by using:

Find: .*"(\d+)"

Replace: $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. 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
  • 6,577
  • 6
  • 27
  • 49
  • 20
    For the replace, `$1` didn't work for me. I used `\1` instead and that worked. – Jason Wheeler Apr 24 '14 at 00:02
  • 13
    `\1` Works in all versions of Notepad++. `$1` only works in the newer ones. – Cullub Sep 23 '14 at 03:01
  • I'm guessing this doesn't work with the `Hex Editor` plugin. In hex view mode Notepad++ doesn't show the `Regular Expression` option for Search/Replace. In text view mode I do see the `Regular Expression` option for Search/Replace. I'm using Notepad++ Version 6.9.2, which is the current version at this time. – Kevin Fegan Aug 23 '16 at 01:50
15

psxls gave a great answer but I think my Notepad++ version is slightly different so the $ (dollar sign) capturing did not work.

I have Notepad++ v.5.9.3 and here's how you can accomplish your task:

Search for the pattern: value=\"([0-9]*)\" And replace with: \1 (whatever you want to do around that capturing group)

Ex. Surround with square brackets

[\1] --> will produce value="[4]"

dchayka
  • 1,201
  • 11
  • 19
  • 1
    `\1` Helped me to keep the number but remove the quote behind it. I had arround 7400 lines in a document like `tablename.columnname = '12345'`. The first quote was easy to remove, the second was done using your `\1` in the replace textbox. Thanks, +1 – Ben Fransen Jun 03 '15 at 10:16
7

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

With $1x$2

Where x is your "value inside scopes".

marsze
  • 11,092
  • 5
  • 33
  • 50
0

I have Notepad++ v6.8.8

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

Replace: [\'\1\']

Will produce: $array[XYZ] => $array['XYZ']

-3

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

Replace: \1

It will really return you

4
403
200
201
116
15

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);
BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
user3178007
  • 41
  • 1
  • 4
  • 3
    How does this JavaScript solution relate to the question which was tagged with Notepad++? – Jason Aller Jun 07 '14 at 02:47
  • How does faulty, broken responses 1 and 2 may relate to the question which was tagged with Notepad++? – user3178007 Jun 07 '14 at 11:00
  • Why I asked the question here? Because I can not create questions. Because I have not enough reputation score. – user3178007 Jun 07 '14 at 11:27
  • 1
    There is no such reputation requirement for asking questions as you claim - you *could not* possibly have been restricted. Try again: http://stackoverflow.com/questions/ask – BoltClock Jun 10 '14 at 14:24
  • The question was asked. http://stackoverflow.com/questions/24112018/find-multiple-words-in-one-line-using-notepad But limited in words to search for. Limitation is! – user3178007 Jun 12 '14 at 11:26
  • You can not - IIS, notepade++, Multiple and all over global words. Nobody finds. Yet. But there are links - http://wefixbugs.com/blog.php?askid=50119#.U5mQC_l_tv8 – user3178007 Jun 12 '14 at 11:39