D
D
Dmitry2015-06-04 13:19:43
Regular Expressions
Dmitry, 2015-06-04 13:19:43

How to replace a group of characters with one in the same amount?

There are strings of the form
927 0000000 927 0199999
901 8020000 901 8039999
902 2900000 902 2999999
902 3200000 902 3249999
902 3350000 902 3399999
...... It
is necessary to change the groups of zeros and nine to% to be changed
, in the first line you need 927 %%%%%%%% % 927 01%%%%%
I sort of figured out how to find such groups: (\d)\1{4,7}
Can be used not only under notepad++

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tplus, 2015-06-04
@tplus

In python it can be done like this

>>> a
'927 0000000 927 0199999'
>>> re.sub('\d{7}', lambda x: '%'*len(x.group()), a)
'927 %%%%%%% 927 %%%%%%%'
>>>

A function that is used to replace each instance of the found sequence. The function takes the result of the expression, extracts the string and returns the number of "%" characters equal to its length.
There is a plugin in npp (I don’t remember the exact name, in the spirit of PythonEditor) that allows you to access the contents of the tab using Python and its API.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question