Parameterize block of text using Notepad++ - regex

I have the following text in Notepad++
A
B
C
D
I would like to "parameterize" this text and turn it into this using a regex or some other native Notepad++ command(s) or plugin:
'A', 'B', 'C', 'D'
Note that I want the end text to be on one line and no trailing comma, if possible. This question gets me close but I am left with a trailing comma and the text is not compacted to one line. Is there anyway to accomplish this in Notepad++ without using a macro?

Try this in Regex Search Mode.
Search for (\w)\r\n
Replace with ('\1', )
But you will have to remove the space and a comma manually from the end of the line.

You can do it in two steps:
Search for e.g. (\w+) and replace with '$1'
The \w+ will find the letters (and digits and the underscore), at least one.
Search for (\s+) and replace with ,
\s+ will find whitespace characters, that means here the newline characters at the end of a row. If you have whitespace in your text, you want to keep, use [\r\n]+ instead.
This way, if there is no newline after the last letter, there will be no trailing comma.

Related

Replace Certain Line Breaks with Equivalent of Pressing delete key on Keyboard NotePad++ Regex

Im using Notepad++ Find and replace and I have regex that looks for [^|]\r which will find the end of the line that starts with 8778.
8778|44523|0||TENNESSEE|ADMINISTRATION||ROLL 169 BATCH 8|1947-09-22|0|OnBase
See Also 15990TT|
I want to basically merge that line with the one below it, so it becomes this:
8778|44523|0||TENNESSEE|ADMINISTRATION||ROLL 169 BATCH 8|1947-09-22|0|OnBase See Also 15990TT|
Ive tried the replace being a blank space, but its grabbing the last character on that line (an e in this case) and replacing that with a space, so its making it
8778|44523|0||TENNESSEE|ADMINISTRATION||ROLL 169 BATCH 8|1947-09-22|0|OnBas
See Also 15990TT|
Is there any way to make it essentially merge the two lines?
\r only matches a carriage return symbol, to match a line break, you need \R that matches any line break sequence.
To keep a part of a pattern after replacement, capture that part with parentheses, and then use a backreference to that group.
So you may use
([^|\r])\R
Replace with $1. Or with $1 if you need to append a space.
Details
([^|\r]) - Capturing group 1 ($1 is the backreference that refers to the group value from the replacement pattern): any char other than | and CR
\R - any line break char sequence, LF, CR or CRLF.
See the regex demo and the Notepad++ demo with settings:
The issue is you're using [^|] to match anything that's not a pipe character before the carriage return, which, on replacement, will remove that character (hence why you're losing an e).
If it's imperative that you match only carriage returns that follow non-pipe characters, capture the preceding character ([^|])\r$ and then put it back in the replacement using $1.
You're also missing a \n in your regex, which is why the replacement isn't concatenating the two lines. So your search should be ([^|])\r\n$ and your replace should be $1.
Find
(\r\n)+
For "Replace" - don't put anything in (not even a space)

Select text between brackets starting with ^{text to be selected} using regex

Eg: sdadjaskdhas ^{sdassad ^{/frac{}{} s dcds &} dsdsadsa} ddsfsafdsfs
Answer should be: {sdassad ^{/frac{}{} s dcds &} dsdsadsa}
it should include any character between brackets
Use backspace to escape the ^,{,} characters as they belong to regex. .* to select any number of any characters.
Here is your answer: \^\{.*\}
EDIT:
You can use this regex [^\^]\^{(\S)} , print the match, trim the string. Loop over this you find all matches in a string.

Notepad++ weird bug? when replace a huge string

I'm getting a CR LF characters after replacing a huge string with Notepad++.
Moreover, The string add a line break in places which I didn't ask.
Weird...
Here is the print screen:
Those CR LF character haven't been there before I was using string replace (or they where hidden? and if so why the string replace revealed them?)
Is there is a quick (regex?) solution to remove them ?
Is there any quick (regex?) solution to remove any characters that is NOT [a-z] [A-Z] [0-9] ["|'] OR NON UTF-8 characters ?
You can just replace \r\n with nothing, and that will remove the line breaks.
To remove any character that is not [a-z][A-Z][0-9]["|'], replace [^A-Za-z0-9"|'] with nothing. But be careful that you've thought of everything you do want to keep: spaces, tabs, other punctuation, etc.

How do I replace a word with a new line and a word using regex with an empty string in Powershell?

How do I replace a word with a new line and a word using regex with an empty string in Powershell?
Below is a sample content... I need to delete all the use database and go I'm using powershell and powershell_ise for editor:
use database_instance
go
if condition
You need to match Newline and also space after the newline:
/use database_\w+\n\s*\w+/g
$sql = #"
use database_instance
go
if condition
"#
$sql -ireplace 'use\s+\w+_\w+\s*(?:\r?\n)+\s*go' , ''
How this Works:
Using -ireplace for case insensitive regex.
Find the word use followed by one or more whitespace \s+ followed by one or more word characters \w+, then an underscore _.
One or more word characters \w+, followed by 0 or more whitespace (just in case)
A non-capturing group (?:) since we don't need the result, this is just to encapsulate a newline that accounts for windows and unix line endings. It consists of an optional CR followed by a LF, and this is matched 1 or more times.
Followed by 0 or more whitespace \s* then the word go.
Replace it with nothing!
This does leave some empty space, but that shouldn't be too big of an issue since the SQL parser won't care.
Note
In your comments you said you tried:
$out -replace "/use database_\w+\n\w+/g"
Be aware that powershell does not use /regexhere/ syntax. The forward slashes are treated as literals, so the flags you specified are as well. The replace is global by default so you don't need g anyway.

How do I remove trailing whitespace using a regular expression?

I want to remove trailing white spaces and tabs from my code without
removing empty lines.
I tried:
\s+$
and:
([^\n]*)\s+\r\n
But they all removed empty lines too. I guess \s matches end-of-line characters too.
UPDATE (2016):
Nowadays I automate such code cleaning by using Sublime's TrailingSpaces package, with custom/user setting:
"trailing_spaces_trim_on_save": true
It highlights trailing white spaces and automatically trims them on save.
Try just removing trailing spaces and tabs:
[ \t]+$
To remove trailing whitespace while also preserving whitespace-only lines, you want the regex to only remove trailing whitespace after non-whitespace characters. So you need to first check for a non-whitespace character. This means that the non-whitespace character will be included in the match, so you need to include it in the replacement.
Regex: ([^ \t\r\n])[ \t]+$
Replacement: \1 or $1, depending on the IDE
The platform is not specified, but in C# (.NET) it would be:
Regular expression (presumes the multiline option - the example below uses it):
[ \t]+(\r?$)
Replacement:
$1
For an explanation of "\r?$", see Regular Expression Options, Multiline Mode (MSDN).
Code example
This will remove all trailing spaces and all trailing TABs in all lines:
string inputText = " Hello, World! \r\n" +
" Some other line\r\n" +
" The last line ";
string cleanedUpText = Regex.Replace(inputText,
#"[ \t]+(\r?$)", #"$1",
RegexOptions.Multiline);
Regex to find trailing and leading whitespaces:
^[ \t]+|[ \t]+$
If using Visual Studio 2012 and later (which uses .NET regular expressions), you can remove trailing whitespace without removing blank lines by using the following regex
Replace (?([^\r\n])\s)+(\r?\n)
With $1
Some explanation
The reason you need the rather complicated expression is that the character class \s matches spaces, tabs and newline characters, so \s+ will match a group of lines containing only whitespace. It doesn't help adding a $ termination to this regex, because this will still match a group of lines containing only whitespace and newline characters.
You may also want to know (as I did) exactly what the (?([^\r\n])\s) expression means. This is an Alternation Construct, which effectively means match to the whitespace character class if it is not a carriage return or linefeed.
Alternation constructs normally have a true and false part,
(?( expression ) yes | no )
but in this case the false part is not specified.
[ |\t]+$ with an empty replace works.
\s+($) with a $1 replace also works, at least in Visual Studio Code...
To remove trailing white space while ignoring empty lines I use positive look-behind:
(?<=\S)\s+$
The look-behind is the way go to exclude the non-whitespace (\S) from the match.
To remove any blank trailing spaces use this:
\n|^\s+\n
I tested in the Atom and Xcode editors.
In Java:
String str = " hello world ";
// prints "hello world"
System.out.println(str.replaceAll("^(\\s+)|(\\s+)$", ""));
You can simply use it like this:
var regex = /( )/g;
Sample: click here