How to Create Variable Definitions from Phrases using Regexes (Notepad++) - regex

Suppose we have a list of phrases - words separated by spaces. And suppose we want to define a bunch of variables based on these phrases such that the following hold:
Phrases already exist and are surrounded by quotes (if not, you can easily use a regex to achieve this)
Phrases only contain letters (this actually isn't true for me in practice, but I can handle those cases manually)
Variable name, followed by an equals sign, should precede the phrase
Variable name should be a lowerCamelCase version of the phrase
Example
Input
"hello World"
"foo bAr"
Expected Output
helloWorld = "hello World"
fooBar = "foo bAr"
Use Case
Often in my line of work I am presented with a bunch of constants which come from an Excel spreadsheet and I need to define a bunch of variables in code for them. The phrases have spaces in them, but the variables can't. I'd usually like to keep the variable names as close to the phrases as I can. I'd like a way to do it in bulk, without having to individually type out each variable name.
Notes
I have come up with a way to do this, which I want to record here in case I need it in future and in case others might need it. I also want to post it here because I have a feeling there are optimizations that can be made to my process, or at least alternatives.

I haven't found a single find/replace step that'll do everything for you, but I have managed to do it using a sequence of regexes, applied one after another. The first pulls out the content for the variable name and inserts the "=". The next one does the main heavy lifting and removes spaces and applies the correct casing. The final one ensures all variables begin with lowercase letters. Apply them in sequence to achieve the desired result.
Regex #1
All we're doing here is pulling content out of the quotes and inserting it on the left hand side.
Note: here and below, I need to use this character for whitespace because SO doesn't render it correctly: ␣. So replace that with a space when you use this or other regexes in this answer.
Find: "(.+)"
Replace: ␣\1 = "\1" (note the leading space)
In our example, after this step, we end up with:
hello World = "hello World"
foo bAr = "foo bAr"
Regex #2
Here, we want to match each word on the left hand side, with the goal of removing the whitespace and simultaneously fixing the casing.
Find: ␣(\S)(\S+)(?=.*=) (note the leading space)
Replace: \u\1\L\2 (absence of space in replacement pattern achieves the removal of the space)
After this step, we end up with:
HelloWorld = "hello World"
FooBar = "foo bAr"
Correct, except for the first letter of each variable name.
Regex #3
This fixes the leading characters to be lowercase:
Find: ^(.)
Replace: \l\1
After this step, our output is as desired:
helloWorld = "hello World"
fooBar = "foo bAr"
Optional Regex #4 (Remove Invalid Characters)
Though the requirement assumed all letters, this is often not the case. First, you may want some numbers in there. Second, there may be junk like parentheses. In this case, just do a find/replace with the replace expression empty for the following find expression:
[^\w\r\n](?!=)(?=.*=)
What that does is first matches negatively to anything that's not a letter, a digit, an underscore or an end of line character. It then ensures that the match is followed by an = down the line but not immediately followed by an =, meaning the space before the = is preserved.
As a Macro
Rather than manually do all 4 steps above, you can record them as a macro and save it to your Notepad++. Or just paste the XML below inside the <Macros> XML element in the file shortcuts.xml inside %appdata%\Notepad++. If you do paste, the shortcut is ctrl+alt+shift+V, but you can change that to whatever you want:
<Macro name="DefineVariables" Ctrl="yes" Alt="yes" Shift="yes" Key="86">
<Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1601" wParam="0" lParam="0" sParam="(.+)" />
<Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
<Action type="3" message="1602" wParam="0" lParam="0" sParam=' \1 = "\1"' />
<Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
<Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
<Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1601" wParam="0" lParam="0" sParam=" (\S)(\S+)(?=.*=)" />
<Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
<Action type="3" message="1602" wParam="0" lParam="0" sParam="\u\1\L\2" />
<Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
<Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
<Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1601" wParam="0" lParam="0" sParam="^(.)" />
<Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
<Action type="3" message="1602" wParam="0" lParam="0" sParam="\l\1" />
<Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
<Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
<Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1601" wParam="0" lParam="0" sParam="[^\w\r\n](?!=)(?=.*=)" />
<Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
<Action type="3" message="1602" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
<Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
</Macro>

Related

IIS rewrite rule one query parameter with multiple values

IIS web.config:
I am trying to redirect a path like
mysite.com/?myparam=123,321,112
to
mysite.com/mypage?param=123,321,112
<rule name="Multiple QueryValue String Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="myparam=(.\,)" />
</conditions>
<action type="Redirect" url="/mypage?param={C:0}" appendQueryString="false" />
</rule>
But whatever i try, it will only forwards the first param. leading to
/mypage?param=123
I have tried multiple combination of {C} and pattern regex.
IMPORTANT: before try something new remember ALWAYS to clear the cache of browser! it's mandatory.
Now...
the pattern you use is not correct, you have to use one suggested by Wiktor Stribiżew
So using pattern="myparam=([0-9\,]*) with url="/mypage?param={C:1}" try this:
<rule name="Multiple QueryValue String Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="myparam=([0-9\,]*)" />
</conditions>
<action type="Redirect" url="/mypage?param={C:1}" appendQueryString="false" />
</rule>
If that is not correct there is something wrong in "," character... something about encoding?
if you try this one what happened?:
<rule name="Multiple QueryValue String Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="myparam=([0-9]*)\,([0-9]*)\,([0-9]*)" />
</conditions>
<action type="Redirect" url="/mypage?param={C:1},{C:2},{C:3}" appendQueryString="false" />
</rule>
If only for mysite.com/?myparam=123,321,112, you can try the following URL rewriting rules:
<rule name="Multiple QueryValue String Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="myparam=(.*)" />
</conditions>
<action type="Redirect" url="/mypage?param={C:1}" appendQueryString="false" />
</rule>

Regexp recursive find/replace in Notepad++

I am wondering if it is possible to write a Regular Expression in Notepad++, allowing me to modify an SQL INSERT script into DELETE.
Below is an example of what I am trying to do.
Input:
INSERT INTO table1 (xxxx, yyyy, zzzz, ....) VALUES (blah1111, foo2222, 3333333, ....);
Output:
DELETE FROM
table1
WHERE
AND xxxx = 'blah1111'
AND yyyy = 'foo2222'
AND zzzz = '3333333'
AND ....;
I have tried to use recursion, but i don't know how to properly make references for each recursion step.
My actual RegEx:
Find script: ((\w+)(?R)?, )
Replace script: (?1) =
I don't know how to do write recursive regular expressions in Notepad++, but you can use macros to automate at least part of the conversion. First you can run the RegEx
Find what: insert into (\w+) (\([^)]*\)) values (\([^)]*\));
Replace with: \2\n\3\nDELETE FROM\n\1\nWHERE\n
to get the basic structure of the DELETE script with two buffer lines above. Then you click Macro -> Start Recording and press Ctrl-Home, right arrow, Ctrl-Shift-right arrow, Ctrl-x, delete, delete, Ctrl-End, Tab, Ctrl-V, =, ', to move the first table column's name in the right position and continue until you have the
WHERE
AND xxxx = 'blah1111'
correctly. Then you click on Stop Recording and playback the macro until all the lines are generated correctly.
You can also try to add my recorded macro to %APPDATA%\Notepad++\shortcuts.xml directly:
<Macro name="ReorderSQL" Ctrl="no" Alt="no" Shift="no" Key="0">
<Action type="0" message="2316" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2306" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2442" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2177" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2180" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2180" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2318" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2327" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2179" wParam="0" lParam="0" sParam="" />
<Action type="1" message="2170" wParam="0" lParam="0" sParam=" " />
<Action type="1" message="2170" wParam="0" lParam="0" sParam="=" />
<Action type="1" message="2170" wParam="0" lParam="0" sParam=" " />
<Action type="0" message="2316" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2306" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2300" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2442" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2177" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2180" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2180" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2318" wParam="0" lParam="0" sParam="" />
<Action type="1" message="2170" wParam="0" lParam="0" sParam="&apos;" />
<Action type="0" message="2179" wParam="0" lParam="0" sParam="" />
<Action type="1" message="2170" wParam="0" lParam="0" sParam="&apos;" />
<Action type="1" message="2170" wParam="0" lParam="0" sParam="
" />
<Action type="1" message="2170" wParam="0" lParam="0" sParam="
" />
</Macro>

IIS - Configuration file is not well-formed XML

<rule name="Posts2" enabled="true">
<match url="^index.cfm\?section=latest.news&id=(.*)" />
<action type="Rewrite" url="post.php?id={R:1}" />
</rule>
Problem is with the regex - any idea at all? I can't seem to work it out. I've run it on http://www.xmlvalidation.com/ and it says
The reference to entity "id" must end with the ';' delimiter.
When I change the ampersand to & the rewrite doesn't seem to work on URLs such as /index.cfm?section=latest.news&id=14726 despite it passing the test in IIS. Even when I escape the dots and question mark it doesn't work
Try using a {QUERY_STRING} input using conditions perhaps:
<rule name="Posts2">
<match url="index\.cfm$" />
<conditions>
<add input="{QUERY_STRING}" pattern="section=(latest\.news)" />
<add input="#{C:1}#_{QUERY_STRING}" pattern="#([^#]+)#_.*id=(\d+)" />
</conditions>
<action type="rewrite" url="post.php?id={C:2}" appendQueryString="false"/>
</rule>
URL's containing query strings are usually better handled this way, although if you really prefer not going this route then make sure you have the all the proper tags included, and also try escaping the . in between latest\.news.
<rewrite>
<rules>
<rule name="Posts2">
<match url="^index\.cfm\?section=latest\.news&id=(\d+)" />
<action type="Rewrite" url="post.php?id={R:1}" />
</rule>
</rules>
</rewrite>

Notepad++ Capitalize Every First Letter of Every Word

Input: "notepad++ capitalize every first letter of every word"
Output: "Notepad++ Capitalize Every First Letter Of Every Word"
I have been attempting to capitalize the first letter of every word using ctr+F and regex.
So far I have been attempting to use find:\b(.) or \<(.) with replace:\u\1 but this results in all of my letters being capitalized.
I have made due with ^(.) & \u\1 followed by \s\b(.) & \u\1.
However, this seems silly to me as there are many posts talking about using the start of word boundaries. I am just having difficulty making them work. Thanks for your consideration!
Background
According to Notepad++ specification (see Substitutions section), there are three operators that can be useful when turning substrings uppercase:
\u
Causes next character to output in uppercase
\U
Causes next characters to be output in uppercase, until a \E is found.
\E
Puts an end to forced case mode initiated by \L or \U.
Thus, you can either match a substring and turn its first character uppercase with \u to capitalize it, or match a character and use \U/\E.
Note that Unicode characters won't be turned uppercase, only ASCII letters are affected.
BOW (Beginning of Word) Bug in Notepad++
Note that currently (in Notepad++ v.6.8.8) the beginning of word does not work for some reason. A common solution that works with most engines (use it in Sublime Text and it will match) does not work:
\b(\w)
This regex matches all word characters irrespective of their position in the string.
I logged a bug Word boundary issue with a generic subpattern next to it #1404.
Solution #1 (for the current Notepad++ v.6.8.8)
The first solution can be using the \w+ and replace with \u$0 (no need using any capturing groups). Though this does not mean we only match the characters at the beginning of a word, the pattern will just match chunks of word characters ([a-zA-Z0-9_] + all Unicode letters/digits) and will turn the first character uppercase.
Solution #2 (for the current Notepad++ v.6.8.8)
The second solution can be implemented with special boundaries defined with lookbehinds:
(?:(?<=^)|(?<=\W))\w
And replace with \U$0\E.
The regex (?:(?<=^)|(?<=\W))\w matches an alphanumeric only at the beginning of a line ((?<=^)) or after a non-word character ((?<=\W)).
The replacement - \U$0\E - contains a \U flag that starts turning letters uppercase and \E is a flag that tells Notepad++ to stop converting case.
Edge case
In case you have hyphenated words, like well-known, and you only want the first part to be capitalized, you can use [\w-]+ with \u$0 replacement. It will also keep strings like -v or --help intact.
A simpler regex that worked for me:
Find: (\w+)
Replace: \u$0
There is a shortcut available in Notepad++ v7.3.2 to capitalize every first letter of every word.
ALT + U
Not sure about prior versions.
Uppercase The First Letter Of Every Word:
Use the shortcut: Alt + U
lowercase the first letter of every word:
Use the shortcut: Clt + U
Shortcut working in version 7.6.3
I have achieved something similar by recording a macro that uses the following replacement.
Find what: ([a-z])+
Replace with: \u$0\E
Tick 'In selection'
This is the resulting macro that I extracted from C:\Users\%USERNAME%\AppData\Roaming\Notepad++\shortcuts.xml.
<Macro name="Title Case" Ctrl="no" Alt="no" Shift="no" Key="0">
<Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1601" wParam="0" lParam="0" sParam="([A-Z])" />
<Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
<Action type="3" message="1602" wParam="0" lParam="0" sParam="\L$0" />
<Action type="3" message="1702" wParam="0" lParam="898" sParam="" />
<Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
<Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1601" wParam="0" lParam="0" sParam="([a-z])+" />
<Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
<Action type="3" message="1602" wParam="0" lParam="0" sParam="\u$0\E" />
<Action type="3" message="1702" wParam="0" lParam="898" sParam="" />
<Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
</Macro>
Extra: you can add this to your right-click context menu (contextMenu.xml) using:
<Item MenuEntryName="Macro" MenuItemName="Title Case" />

Merging two lines into one - Notepad++

I have a line like this
assignee: Akebono Brake Industry Co. Ltd. ,
Fujitsu Limited application_no: 06/946,825
I want the output to be
assignee: Akebono Brake Industry Co. Ltd. , Fujitsu Limited
application_no: 06/946,825
To bring the application_no: 06/946,825 to the next line, I can find application_no: and replace it with \napplication_no: in my NOTEPAD++
But, how can I bring that string that spans to next line back to the first line? I mean what should I do to get the Fujitsu Limited to the line starting with assignee:
Any guidance please?
Since Extended is the only mode that handles the newlines correctly but you need to match with regular expressions, you will need to do this in two steps.
First, use a regex find and replace to add some recognizable token to the beginning of each line you want to move up, I used 'MATCH' but you could definitely change this.
Then, switch to Extended to search for a newline followed by the token, and replace it with an empty string to delete both the line break and the token.
Here is another solution. One step:
Search:
(^.*,.*$)\r\n([ A-Z]*[ ])
Replace:
\1\2\r\n
I am unfamiliar with notepad++, but surely there is a "/n" after the comma? Could you not just remove the char that creates the new line segment? ie: the inverse of what you are doing to application_no:
You can't do that with regular expressions due to a flaw in the Scintilla engine which Notepad++ uses. However, it works in "extended" find mode, so use that. Search for ,\r\n and replace with ,.
Change the \r\n to only \n on Linux, or to only \r on Mac OS.
I've just wrote that macro and works with your example.
Add this macro into shortcuts.xml, if you are using win7 file is located at C:\Users\{username}\AppData\Roaming\Notepad++
Just open your text file and get cursor to firts line, then run this macro.
<Macro name="stackoverflow" Ctrl="no" Alt="no" Shift="no" Key="0">
<Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1601" wParam="0" lParam="0" sParam="application_no" />
<Action type="3" message="1625" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
<Action type="3" message="1701" wParam="0" lParam="1" sParam="" />
<Action type="0" message="2302" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2451" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2306" wParam="0" lParam="0" sParam="" />
<Action type="0" message="2326" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1601" wParam="0" lParam="0" sParam="application_no" />
<Action type="3" message="1625" wParam="0" lParam="0" sParam="" />
<Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
<Action type="3" message="1701" wParam="0" lParam="1" sParam="" />
<Action type="0" message="2308" wParam="0" lParam="0" sParam="" />
<Action type="1" message="2170" wParam="0" lParam="0" sParam="
" />
<Action type="1" message="2170" wParam="0" lParam="0" sParam="
" />
</Macro>