[regex help] Need to change [[ to [ in files
Hello, I am a noob to regex and hopefully somebody will be able to answer this for me.
I need to change instances of “[[” to “[”, but only where the word “remoteExec” is found on the same line (comes after it with stuff in between).
Thanks in advance.
find what: (?=.*)(\[\[)(?=.*remoteExec.*) replace with: [
would to it.
() = is a matching group
(?= ) doesn’t return a match even if it matches
(?=.*) = we are not interested in whatever chars followed by
([[) = [[ (which are of interest) and followed by
(?=.remoteExec.) chars/or not and remoteExec and chars or not (but not of interest)
You wann find out more about regex? See here.
Thank you very much, this saved me so much time going through 400+ files and doing it manually. I really need to learn this stuff.
Hello Kevin and Claudia,
I began, yesterday, to prepare a reply to Kevin, but I didn’t post it, as I was quite tired, due to a three days ski course, in the French station of Val-Cenis ! Seemingly, we, both, had the same idea to search, only, for the two opening square brackets, using a look-ahead to detect the keyword remoteExec, further, on each line :-)) My regex is a bit different but give the same results, at the end !
Indeed, regular expressions can help you, in may ways, to modify your files, doing the changes, repeatedly, without any error, on 10 or 100000 lines ( in almost the same time ), and on one or several files. Of course, you’ll just have to test your regex, on a small part of a real file, first !
As you are a newbie I’ll try to give you some explanations. Then, with some further documentation, you’ll be able to create your own regexes. Once you’ve begun to discover the fascinating power of regular expressions, you won’t go back, anymore :-))
First of all, I’ll use the test text, below, to study the general case :
This are some [[ lines of text remoteExec to find out [[ and test the right remoteExec regular [[ expression [[ This second line of text [[remoteExec to find out [[ and test the right remote[[Exec regular expression A third [[ line of text to find out [ and test the right regular [[ expression remoteExec [[remoteExec ( 4th line )
Notice that :
Line 1 contains 2 correct keywords remoteExec
Line 2 contains a wrong keyword remote[[exec on purpose !
Line 3 has the keyword remoteExec, at the end of the line
Line 4 has no character between the string [[ and the keyword remoteExec
So, to run the S/R, just follow the few steps, below :
Move to the very beginning of your file ( CTRL + Origin )
Open the Replace dialog ( CTRL + H )
Select the Regular expression search mode
Type in the Find what and Replace with zones, the text below :
Find what : (?-si)\[\[(?=.*remoteExec) Replace with : [
- Click on the Replace All button ( or on the Replace button, if the number of occurrences, to modify, is not too important )
Et voilà !
=> You’ll get the changed test text, below :
This are some [ lines of text remoteExec to find out [ and test the right remoteExec regular [[ expression [ This second line of text [remoteExec to find out [[ and test the right remote[[Exec regular expression A third [ line of text to find out [ and test the right regular [ expression remoteExec [remoteExec ( 4th line )
Well. Now, let’s explain, a bit, this regex :
Globally, this regex tries to match two consecutive opening square brackets and replace these two characters, with an unique opening square bracket, placed in the replacement part
But as the opening square bracket has a special meaning, in regexes, we need to escape each
[character with an antislash character, in order to search for this character, literally. ( For instance, the regex
[0-9]looks for a digit, between 0 and 9, as the regex
\[0-9\]would look for the exact string [0-9] ).So, the two consecutive [[ characters, can be searched with the regex
(?=.*remoteExec), is a look-around form, which is a condition that must be true, in order that the regex engine consider the complete regex matched. However, the text matched in that condition, is never part of the final text matched ! ( A simple example : the regex
abc(?=def)matches the string abc, ONLY, IF immediately followed with the string def ) Then, in our regex, the final text, to search for, is only the two opening square brackets !
In our regex, the condition is the regex
., in regexes, is a meta-character that represents any single standard character, different from the EOL characters ( Usually, the characters New Line,
\nand Carriage Return
*, in regexes is, also a meta-character, called a quantifier, which means any repetition, of the previous character, EVEN 0 ( For instance, the regex
0,0*1matched the strings 0,1, 0,01, 0,001, and so on… ).
Therefore, in our regex, the syntax
.*, followed with the literal text
remoteExec, stands for any range, even empty, of standard characters, between the two [[ characters and the string remoteExec
Finally, the form
(?-si), at the beginning of the regex, which could have been written
(?-s)(?-i), represents two modifiers :
-ssyntax ( No Single line ) means that the dot meta-character matches ONLY the standard characters, even if you checked, previously, the . matches newline option, in the Replace dialog
-isyntax ( No Insensitive) means that the regex
remoteExecwith match, exclusively, the string remoteExec, and not similar strings as RemoteExec, Remoteexec or, even, REMOTEEXEC, even if you DON’T check the Match case option
If this keyword can have several case forms, in your file, you’ll use, instead, the syntax
sare never mandatory. So, the search regex could be, simply,
\[\[(?=.*remoteExec)But, in that case, for the regex works properly, you’ll need to :
Check the Match case option
Uncheck the . matches newline option
You’ll find good documentation, about the new Boost C++ Regex library, v1.55.0 ( similar to the PERL Regular Common Expressions, v1.48.0 ), used by
Notepad++, since its
6.0version, at the TWO addresses below :
The FIRST link explains the syntax, of regular expressions, in the SEARCH part
The SECOND link explains the syntax, of regular expressions, in the REPLACEMENT part
You may, also, look for valuable informations, on the sites, below :
To end with, feel free to ask, the N++ Community, for infos on any tricky regex that you came across OR for building any tricky regex, for a particular purpose :-)) As I said above : an other universe !!
Skiing in the alps
having 300 MBit internet connection
and the patience to write such detailed answers,
there must be something different in the french air
maybe I should think of moving to france ;-)