-1

I've been trying to separate all numbers in a text file from the words, then run them through a formula in excel and batch replace them using a method similar to this in Notepad++.

In my situation, this doesn't work, as recurring numbers occur throughout the text and as such the search and replace gets stuck in an infinite loop no matter what I try to do. Especially when there are decimals AND integers involved, as Notepad++ cannot logically distinguish between 1.14, 1 and 14, so it gets messy.

Is there any way to do this directly in Notepad++ or somewhere online? Or, alternatively, is there a way to extract numbers from text, maintaining their space within the text, being able to edit all the values and then plug them back into their original positions in the document?

Edit:

I want to turn:

<OptionNumeric name='vnlaClayFreq' default='1' min='0' max='5' </OptionNumeric>

<Setting name='MotherlodeSize' avg=':= 1.648 * _default_ * vnlaClaySize ' range=':= 1.648 * _default_ * vnlaClaySize ' type='normal' />

<Setting name='MotherlodeHeight' avg=':= 64 ' range=':= 10 ' type='uniform' scaleTo='seaLevel' />

Into:

<OptionNumeric name='vnlaClayFreq' default='1' min='0' max='3' </OptionNumeric>

<Setting name='MotherlodeSize' avg=':= 0.824 * _default_ * vnlaClaySize ' range=':= 0.824 * _default_ * vnlaClaySize ' type='normal' />

<Setting name='MotherlodeHeight' avg=':= 32 ' range=':= 5 ' type='uniform' scaleTo='seaLevel' />

Where integers remain integers, getting rounded to the nearest whole number if necessary (i.e. 1 remains 1), while decimals get altered just as Toto's python code does (i.e. 1.0 becomes 0.5).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
kyozo_43
  • 1
  • 2

1 Answers1

0

You can run a python script within the PythonScript plugin.

If it is not yet installed, follow this guide

Create a script (Plugins >> PythonScript >> New Script)

Copy this code and save the file (for example calculate.py):

import re           # regular expression package
import math         # math functions package

# define function calculate
def calculate(match):
    # if group 2 exists (it is an integer)
    if (match.group(2)):
        # round up (ceil) the group 2 divided by 2 and return an integer
        res = int(math.ceil(float(match.group(2))/2))
    else:   # group 2 doesn't exist, so process group 1 (float)
        # divide group 1 by 2
        res = float(match.group(1))/2
    # return the result of the calculation
    return res

# replace in editor window what is matched by regex by a call to calculate function
editor.rereplace('(\d+\.\d+)|(\d+)', calculate)
# regex
# (\d+\.\d+) : group 1, matches a float
# | : OR (regex operator)
# (\d+) : group 2, matches an integer
  • Open the file you want to change
  • Run the script (Plugins >> PythonScript >> Scripts >> calculate)
  • Done

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Toto
  • 89,455
  • 62
  • 89
  • 125
  • Oh shoot, actually one thing that I wanted it to do was also differentiate between integers and decimals. So I want the output to be "between 0.57, 1 and 7, so it gets messy." (i.e. round the integers to the nearest whole number, and maintain integers as "7" rather than "7.0". Is this possible? – kyozo_43 Jun 09 '20 at 03:23
  • Amazing! Works perfectly. Do you care to explain why/how it works? I don't quite understand the "import" and "def" functions in particular. But an explanation on the whole script would be most insightful! – kyozo_43 Jun 09 '20 at 13:34
  • @kyozo_43: I've added some explanations in the python code. If it works for you, feel free to mark the answer as accepted, [How to accept an answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Toto Jun 09 '20 at 15:47
  • Ah, thanks. First time poster so didn't realise I needed to accept the answer! – kyozo_43 Jun 10 '20 at 04:56