1

I need help with Find and Replace in Notepad++.

Example:

$250 'Hello 1' Take '____'

$500 'Hello 2' Take '____'

$1000 'Hello 3' Take '____'

Imagine there are thousands of these entries going up to 'Hello 9999'.

I want to replace '____' with a number without changing 'Hello 1' or 'Hello 2'.

I would like the outcome to be:

$250 'Hello 1' Take '250'

$500 'Hello 2' Take '500'

$1000 'Hello 3' Take '1000'

How can I go about doing this? I would expect using a regular expression.

3 Answers 3

2

Tick the regular expression checkbox and use:

Search:

\$(.*?) (.*?) Take '(.*?)'

Replace:

\$$1 $2 Take '$1'

This will search for a line who basically looks like this:

$anything anything Take 'anything'  

.*? means any character except a new line in regular expressions.
Wrapping parenthesis around a capture group such as .*? will store the content of it in a variable which is $n where n is the n-th group.

So after the search we will have three variables:
$1 = 250 $2 = 'Hello 1' $3 = ___

With the replace, we replace the actual whole line with \$ (escaping the dollar sign makes it a litteral $ and not a variable).
The whole line is replaced by $$1 $2 '$1'
As you can see we use again the $1 variable who is the first capturing group.


I'm quite new on SO, so sorry if my explanations aren't that great.

3
  • I don't quite understand, would you mind explaining how this method works? Thanks.
    – User162636
    Jun 10, 2016 at 23:36
  • Sure, sorry. I should have explained in my original post. I'll update it right now.
    – Greg01re
    Jun 11, 2016 at 11:07
  • Thanks for the explanation, how would I apply this solution to a multi line document? I have an example here: hastebin.com/efohesonis.sm Where 'Price ' will be duplicated to 'Coins:____' and 'give:material:28' will be saved.
    – User162636
    Jun 11, 2016 at 13:06
1

This should be the Notepad++ syntax, having selected the "Regular Expression" radio button at the bottom of the Find/Replace dialog:

Find:  (\$(\d+).*Take\s+').*(')
Replace:  \1\2\3

Explanation:

Parenthesis group variables in order by counting from left to right:

(\$(\d+).*Take\s+').*(')
|--------1--------|
   |-2-|             |3|

\$ : means find dollar sign

\d+ : means find digits, "+" at the end means one or more (i.e. MUST be at least one digit).

.* : "." means ANY character, and "*" means one or more.

Take : is just specifically the word Text.

\s+ : means space characters, and "+" means one or more.

...

Now, because everything is grouped EXCEPT the last characters in the single quotes, then:

\1\2\3

Just puts the first and third parenthesis, with the second in between (where the second parenthesis is the dollar value).

2
  • Thanks for the explanation, how would I apply this solution to a multi line document? I have an example here: hastebin.com/efohesonis.sm Where 'Price ' will be duplicated to 'Coins:____' and 'give:material:28' will be saved.
    – User162636
    Jun 11, 2016 at 13:08
  • That example looks like yaml. Do you have any scripting ability? Maybe use Python/Perl/Ruby, or even a crude sed script, would give you more power and flexibility than regex in Notepad++. ... However, if you must use Notepad++ regex, there is a checkbox that should read something like ". matches newline". Select this checkbox, so multiline matches can be performed. Then update the regex to match up to "Coins:" with some other modifiers, probably "?" to make the regex non-greedy.
    – jehad
    Jun 11, 2016 at 14:11
0

Try This

First turn on Regular expressions

Notepad++ Settings to turn on Regular Expressions

https://regex101.com/r/aW4gG2/2

Find: (\$\d+)(.*Take ')(.*)(')
Replace: \1\2\1\4

Capture

There are four groups to this Expression

Input: $250 'Hello 1' Take '____'

Group 1
(\$d+) = $250 - this says start with a $ (escaped to be literal) then digits (\d) 1 or more so it stops at the last digit whether it's 3 or 15. In your example they were at the start of the line but I don't assume that. If however they DO start the line then you might want to add a ^ making it ^(\Sd+)

Group 2
(.*Take ') = 'Hello 1' Take ' - the expression says .* which means any character any number of times so it take everything until it sees Take and then it takes the Take ' because i wrote that out and stops there.

Group 3
(.*) = - ____ this takes the internal space basically it takes anything 0 or more times. In this case it's all underscores but written it would grab everything and anything you have there. It would grab everything to the end of the lines except I have something following which tells it when to stop

Group 4
(') = - ' this tells group 3 to stop at the ' mark. it also captures the ' mark in it's own group for rearranging.

Substitution

\1\2\1\4

Groups are numbered from their open parenthesis but in this case everything is linear with no nesting so it's pretty obvious. \1\2\3\4 would leave everything the way it is. But we want to replace the third group with the first group so.... \1\2\1\4 does the trick. One of the key points here was making sure we captured that fourth group to replace it at the end. Beyond that nothing too tricky.

Execution

So just set that up in your find replace and then hit replace all and that should fix you up.

Edit

I noticed you wanted to strip the $ in your replacement so you need to make some minor changes to my initial suggestions though it's basically the same.

Input: $250 'Hello 1' Take '____'

Find: (\$)(\d+)(.*Take ')(.*)(')
Replace: \1\2\3\2\5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.