Skip to content

Commit e2bc863

Browse files
committed

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

scripts/Samples/Multiples_SR.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#coding=utf-8
2+
3+
import re
4+
5+
# --------------------------------------------------------------------------------------------------------------------------------------
6+
7+
# Script "Multiples_SR.py"
8+
9+
# A LITTLE adaptation by guy038 from an ORIGINAL and VALUABLE script of Alan KILBORN ( January 2019 ) !
10+
11+
# See https://notepad-plus-plus.org/community/topic/16942/pythonscript-any-ready-pyscript-to-replace-one-huge-set-of-regex-phrases-with-others/21
12+
# and https://notepad-plus-plus.org/community/topic/16942/pythonscript-any-ready-pyscript-to-replace-one-huge-set-of-regex-phrases-with-others/23
13+
14+
# This script :
15+
16+
# - Reads an existing "SR_List.txt" file, of the CURRENT directory, containing a list of SEARCH/REPLACEMENT strings, ONE PER line
17+
# - Selects, one at a time, a COUPLE of SEARCH and REPLACEMENT regexes / expressions / strings / characters
18+
# - Executes this present S/R on CURRENT edited file, in NOTEPAD++
19+
# - Loop till the END of file
20+
21+
# Any PURE BLANK line or COMMENT line, beginning with '#', of the "SR_list.txt" file, are simply IGNORED
22+
23+
# --------------------------------------------------------------------------------------------------------------------------------------
24+
25+
# For EACH line, in the "SR_List.txt" file, the format is <DELIMITER><SEARCH regex><DELIMITER><REPLACE regex><DELIMITER>
26+
27+
## EXAMPLES :
28+
## ¯¯¯¯¯¯¯¯
29+
30+
## Deletes any [ending] "; comment" / Delimiter = '!'
31+
#!(?-s)(^.*?);.+!\1!
32+
33+
## Changes any LOWER-case string "notepad++" in its UPPER-case equivalent / Delimiter = '@'
34+
#@(?-i)notepad\+\+@NOTEPAD++@
35+
36+
## Changes any "Smith" and 'James' strings, with that EXACT case, to, respectively, "Name" and "First name" / Delimiter = '&'
37+
## Deletes any "TEST" string, with that EXACT case
38+
#&(Smith)|TEST|(James)&(?1Name)(?2First name)&
39+
40+
## Replaces any BACKSLASH character with the "123" number, both preceded and followed with 3 SPACE characters / Delimiter = '%'
41+
#%\\% 123 %
42+
## or, also, the syntax %\x5c% 123 %
43+
44+
## Deletes any string "Fix", followed with a SPACE char, whatever its CASE / Delimiter = '+'
45+
#+(?i)Fix ++
46+
47+
## Change 3 CONSECUTIVE "#" characters with 3 BACKSLASH characters / Delimiter = '*'
48+
#*###*\\\\\\*
49+
50+
# --------------------------------------------------------------------------------------------------------------------------------------
51+
52+
# In the CODE line, right below, you may :
53+
54+
# - Modify the NAME of the file, containing the SEARCH and REPLACEMENT regexes
55+
# - Indicate an ABSOLUTE or RELATIVE path, before the filename
56+
57+
with open(r'SR_list.txt') as f: sr_list = f.readlines()
58+
59+
# You may, as well, insert the SEARCH and REPLACE regexes, directly, in THIS script :
60+
61+
#sr_list = [
62+
# '!(?-s)(^.*?);.+!\\1!',
63+
# '@(?-i)notepad\\+\\+@NOTEPAD++@',
64+
# '&(Smith)|TEST|(James)&(?1Name)(?2First name)&',
65+
# '%\\\\% 123 %',
66+
# # or the syntax '%\x5c\x5c% 123 %',
67+
# '+(?i)Fix ++',
68+
# '*###*\\\\\\\\\\\\*',
69+
# ]
70+
71+
# The use of RAW strings r'.......' is also possible, in order to SIMPLIFY some regexes
72+
73+
# Note that these RAW regexes are strictly IDENTICAL to those, which could be contained in a "SR_List.txt" file, WITHOUT the 'r' PREFIX
74+
75+
#sr_list = [
76+
# r'!(?-s)(^.*?);.+!\1!',
77+
# r'@(?-i)notepad\+\+@NOTEPAD++@',
78+
# r'&(Smith)|TEST|(James)&(?1Name)(?2First name)&',
79+
# r'%\\% 123 %',
80+
# # or the syntax r'%\x5c% 123 %',
81+
# r'+(?i)Fix ++',
82+
# r'*###*\\\\\\*',
83+
# ]
84+
85+
editor.beginUndoAction()
86+
87+
console.write ('\nMODIFICATIONS on FILE "{}: "\n\n'.format(notepad.getCurrentFilename()))
88+
89+
# Note : Variable e is always EMPTY string ( Part AFTER the THIRD delimiter and BEFORE the END of line ! )
90+
91+
for line in sr_list:
92+
93+
if line[0] == '#' or line == '\n' : continue
94+
(s,r,e) = line[1:].rstrip('\n').split(line[0])
95+
96+
console.write(' SEARCH : >{}<\n'.format(s))
97+
console.write(' REPLACE : >{}<\n\n'.format(r))
98+
99+
editor.rereplace(s,r) # or editor.rereplace(s,r,re.IGNORECASE) / editor.rereplace(s,r,re.I)
100+
101+
editor.endUndoAction()
102+
103+
# END of Multiple_SR.py script

0 commit comments

Comments
 (0)