4

I'm sorry if this has been covered before, I did try to find a previous question covering this but given the subject of the question it's kinda hard to filter through the thousands of answers to other questions.

I have a very large document that contains values denoted by a "+" or a "-". I need to reverse the values in this file.

I know enough to find and replace multiple characters using regex however I don't know enough to search for characters that are used by regex.

I've tried using quotations and even including the | on either side that appear in the file but obviously this is also used in regex.

Is this even possible, how can I get regex to search for "+"?

EDIT: Using normal find and replace is not the solution as this would leave me with all of one character and none of the other but I need to swap them to reverse the value of the whole file.

Onthrax and markalex have already helped me to fix my regex but I'm still open to other solutions.

x1ras
  • 125
  • 1
  • 9
  • 1
    A + character is a special character in regular expressions. You need to escape it. usually by adding a slash before it. So it becomes \+ – Onthrax Jun 30 '23 at 09:44
  • 1
    To search for + with regex escape it with \, like this: `\+`. But why do you need regex there in the first place? Shouldn't this be solved with simple string replacement? – markalex Jun 30 '23 at 09:44
  • Thank you both! It's clearly too early on a Friday morning for me to be loose on the internet as this is such an obvious solution now that you've said it. – x1ras Jun 30 '23 at 09:53
  • @markalex sorry, I missed the second part of your comment. Unless I'm being **really** dumb this morning, find and replace could replace the +'s for -'s but not swap them so I'd be left with over 30,000 lines with just -'s which would make things worse than they already are. Whereas regex can swap the characters by iteratively working through each occurrence of + or - Although I could be completely wrong, I have already proven that I can't figure out obvious solution on my own. – x1ras Jun 30 '23 at 10:31
  • Can you give an example of the text you have? Would make it a bit easier to test. – Cow Jun 30 '23 at 10:44
  • @x1ras, I'm not sure what you mean by "iteratively working through each occurrence": do you plan to do replacement manually? Regarding mass replace: replace + with something rare, like `€`, then - with +, and finally € with -. – markalex Jun 30 '23 at 10:47
  • @markalex sorry, my terminology is definitely not right (can you tell I'm in over my head?). I meant that regex will find all instances of + and replace them with - whilst simultaneously finding all instances of - and replacing with +, instead of finding all + and replacing and **then** finding all - and replacing. – x1ras Jun 30 '23 at 11:15
  • Ah, I wasn't sure if notepad++ supports optional group replacement, so I proposed multistep idea. But InSync's answer seems to indicate that they are supported by npp, so use it to make replacement in one go. – markalex Jun 30 '23 at 11:24

2 Answers2

5

Replace:

(\+)|-

...with:

(?1-:+)

(\+) matches and captures every + sign whereas - matches, without capturing, every - sign. (?1-:+) means "add - back if group 1 was captured, + otherwise". This effectively swaps the two signs.

Try it on regex101.com (with minor differences in syntax).

Alternatively, you can replace:

  1. every + with a temporary sign
  2. every - with +
  3. every temporary sign used in step 1 with -

This, however, requires the temporary sign we use to not be used in the whole document.

InSync
  • 4,851
  • 4
  • 8
  • 30
0

As pointed out by Onthrax and markalex in the comments, you just need to use \ to escape the regex.

InSync
  • 4,851
  • 4
  • 8
  • 30
x1ras
  • 125
  • 1
  • 9