1

So, I have a lot of numbers in lines like so

rocket123
firefly1000
attack577

Is there any regex to make the numbers reversed?

rocket321
firefly0001
attack775
Caleb
  • 5,084
  • 1
  • 46
  • 65
ramza75
  • 65
  • 1
  • 7

2 Answers2

0

This is feasible with a little trick.

Step 1. Add a marker for the not-yet-inverted digits.

Find:

\b(\w+?)(\d+)\b

Replace:

$1§$2

You can choose other marker instead of §.

Step 2. Do Replace all enough times with these settings:

Find:

\b(\w+)§(\d*)(\d)\b

Replace:

$1$3§$2

Step 3. Delete all markers.

Find:

\b(\w+\d)§

Replace:

$1

Hope this helps.

logi-kal
  • 7,107
  • 6
  • 31
  • 43
  • Yea this seems to work, the # gets added in the lines but that can easily be replaced finding # replacing with nothing, holy shit dude ty! – ramza75 Jul 04 '17 at 21:22
  • @ramza75 This works indeed with every number of digits. Just in step 2 click on "Replace all" enough times, as I've already said. – logi-kal Jul 04 '17 at 21:26
  • You might have better luck with multiple optional capturing groups rather than hitting replace all many times. – Wiktor Stribiżew Jul 04 '17 at 21:27
  • @ramza75 Anyway, please, take a look at https://meta.stackexchange.com/questions/22232 – logi-kal Jul 04 '17 at 21:28
  • @horcrux your right dude, hitting replace a bunch of times works like a charm, my friend you are the best!!!!!!!! – ramza75 Jul 04 '17 at 21:33
  • @horcrux is it possible to invert numbers in between letters? For exampe Jack4ever3 or something like, right now this regex only swaps numbers at the end of a word, like firefly203 to firefly302 – ramza75 Jul 04 '17 at 23:43
  • @ramza75 Do you want to obtain Jack3ever4? – logi-kal Jul 05 '17 at 08:21
  • @horcrux i want all numbers in every line to be backwards, with your regex works only somelines though idk why. I noticed some lines thats start with numbers dont get reversed and some with numbers inbetween dont either, this regex only seems to work with numbers at the end – ramza75 Jul 05 '17 at 10:31
  • @ramza75 The answer satisfies your question according to the examples that you provided in the original post (OP). If now you want something different, ask a new question in a separated thread. P.S. You should also try to do something by yourself, SO is not a coding service. – logi-kal Jul 05 '17 at 11:16
0

If the maximum number of digits to be reversed is known and not too large then a single Notepad++ regular expression search and replace can be used. Suppose the maximum number of digits is 12 then the expressions are:

Search for:

(\d)(\d)(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?(\d)?

Replace with:

(?{12}${12})(?{11}${11})(?{10}${10})(?9$9)(?8$8)(?7$7)(?6$6)(?5$5)(?4$4)(?3$3)$2$1

Explanation:

Any number to be reversed must have at least two digits, so the initial (\d)(\d) in the search gets two digits and the final $2$1 in the replace puts them in reverse order at the end of the output. (The first two digits are the easy part.) The search string then repeats the pattern (\d)? as many times as needed for the maximum number of digits. These match the remaining digits, if any. Each of these (\d)? patterns has a corresponding item in the replace string, they are of the form (?N$N) where each N is the number of the capture group. Single digit captures are like (?4$4) for number 4. For captures 10 and above the number is wrapped in curly braces, such as (?{12}${12}) for number 12. These replacement items test whether a capture group captured anything and, if it did then insert that captured item. See also this answer.

Variations

Add or remove additional search and replacement items as needed for longer or shorter maximum numbers of digits.

If the number of digits might be larger than expected then adding an extra (\d)? to the search string and (?{13}__Some suitable error message__) to the end of the replacement will output the error message on overlong groups of digits. Of course the 13 needs to be altered to match the number of items in search and replacement.

Tested with Notepad++ version 7.5.6.

AdrianHHH
  • 13,492
  • 16
  • 50
  • 87