# Regex: select/match the numbers that are repeated most often browsing

• hello, I have 15 rows with 7 numbers, from 1 to 50. How can I match the 4 numbers that are repeated most often in all those 15 rows?

I suppose I must first select all numbers `\d+`
then, I have to divide all 2-digit numbers `\b[1-9]{2}\b` by all 1-digit numbers `\b[1-9]{1}\b`
or, I should select all numbers from `1-10`, then all numbers from `10-20` …and from `40-50`

I don’t know exactly, there should be a mathematics formula. In Excel I can use filters for this, or Sort from lowest to highest, etc

But how can I do this with regex?

• @Vasile-Caraus

I know you are a regex fan but just to give you an idea how a python script
would look like to solve such a problem

``````from collections import Counter

x = editor.getText().replace('\r\n',' ').split(' ')  # get the list of numbers
y = [y for y in x if y !='']                         # get rid of the empty ones
counted_list = Counter(y)                            # create a list of tuples, counting each
for item in counted_list.most_common(4):             # iterate over the top 4
console.write('{}\n'.format(item))               # and print it to the console
``````

I used the list of 1000 integer @guy038 posted.
The result in the console would be

(‘7’, 45)
(‘27’, 41)
(‘8’, 40)
(‘13’, 40)

Meaning that number 7 occurred 45 times

Cheers
Claudia

• @Claudia-Frank said:

n idea how a pytho

hello Claudia, I don’t know Phyton, so I really don’t know what to do with the phyton script you write above.

• posted

Hello Claudia,

I’ve just tested, your Python solution, changing for the six most common used numbers, with the `counted_list.most_common(6)` expression and it just return all the numbers that I’ve had previously found, for the 1000 random integers list :-)

How elegant a Python ( or Lua, I suppose ) script is, compared to my complicated regex’s cooking !!!

Cheers,

guy038

• Claudia and guy038, please tell me how to use this python script !

• a short tutorial for this example will be great !

• @Vasile-Caraus

What needs to be done first is described here.

Just in case that you haven’t installed python script plugin yet, I would propose to use the MSI package instead of using the plugin manager.

Short version, once python script plugin has been installed goto
Plugins->Python Script->New Script
give it a name and press save.
A new empty editor should appear.
Copy the content into it and save it.
Do NOT reformat the code as python is strict about whitespaces.

Open the python script console by clicking on
Plugins->Python Script->Show Console

Open your file with the numbers and run the script by clicking on
Plugins->Python Script->Scripts->NAME_OF_YOUR_SCRIPT
Cheers
Claudia

• WORKS GREAT Claudia.

Thanks a lot !

• by the way, Claudia, how can I use Python (like your script) to actually modify the .txt file. Because, for now, Python only show in the console the results of some function from the script. But how can I use Python script to search and replace something in the .txt files?

• @Vasile-Caraus

if you want to dive into python first thing, of course, is to get some basic knowledge of the language it self.
Note, the plugin uses python2 NOT 3 (there are differences, nothing too critical but those can be confusing
if you start learning the language and you try to do something which works in py3 but not in py2).

Next the help pages which come with the plugin itself.
Plugins->Python Script->Context-Help

And last but not least Scintillas help at http://www.scintilla.org/ScintillaDoc.html to get a better
understanding how the editor works.

The console is a good starting point to test things first.
In order to get all functions, attributes of a py object you can use the dir command.
So, if you do the following in the console you will get the list of functions of this object

``````dir(editor)
``````

I prefer to have not to scroll sideways so I use

``````print '\n'.join(dir(editor))
``````

In order to see what the parameters of a function are use the help command like

``````help(editor.insertText)
``````

Next if you search the forum you will find many scripts to solve some particular issues
one of my first posts answered a question to unit conversion

and finally, ask the question here if you have a specifc question.

Cheers
Claudia

Ahh… I would suggest to do the following changes in notepad
Settings->Preferences->Language check the “replace by space” because
Python don’t like it if you use tabs and spaces for indentation.

• @Claudia-Frank

Regarding print ‘\n’.join(dir(editor))

I don’t think that ‘print’ outputs to the Pythonscript console window by default.

From the following in the original startup.py:

# This sets the stdout to be the currently active document, so print “hello world”,
# will insert “hello world” at the current cursor position of the current document
sys.stdout = editor

This is of dubious value, especially since a ‘print’ used in this way inserts the text specified plus a UNIX-style line ending into your current file (which likely has Windows-style line endings!).

I, and likely also Claudia, have changed this line in startup.py to be:

sys.stdout = console

thus changing ‘print’ statements to output their data to the Pythonscript console (great for debugging your scripts!)

As alluded to above, the Pythonscript console seems to use UNIX-style line endings. I found this out in an odd way. If you copy-and-paste from the console to an editing window with Windows line endings, the line-endings on the source text will be changed at the time of the paste to match the destination file format, so all is good. HOWEVER, what I did one time was to paste via the “Clipboard History” window. This action seems to preserve the original UNIX-style line endings at the destination! I was quite confused as to why I had inconsistent line-endings in my document, until I figured it out.

• @Scott-Sumner

Scott, you are absolutely correct, I’ve changed this in startup.py
and for me this is much more convenient than using console.write to
print chars to the console.
Just a side not, the command
print ‘\n’.join(dir(editor))
should have been executed in the console itself and there it is working
but if some would use it in a script, than it would print to editor unless
you do changes Scott mentioned.

Thx for the info about copy/paste - I do this a lot but luckily I didn’t use the history ;-)

Cheers
Claudia

Internal error.

Oops! Looks like something went wrong!