Notepad++ Regular Expression Condition Replacement - regex

I have a set of SQL script that wants to change schema.
create table Service.Table1 (col1 varchar(100));
create table Operation.Table2 (col1 varchar(100));
create table Support.Table3 (col1 varchar(100));
However, the schema is going to change
Service -> Sev
Operation -> Opn
Support -> Spt
The search regular expression is easy ([A-Za-z0-9_]+)\.([A-Za-z0-9_]+)
However, how to do the conditional replacement in Notepad++ or other tools if they can?
Thanks!

If you have a predefined set of the schemas, you may use the conditional replacement in Notepad++ like this:
Find: (?:(?<a>Service)|(?<b>Operation)|(?<c>Support))\.(?<n>[A-Z0-9_]+)
Replace: (?{a}Sev:(?{b}Opn:Spt)).$+{n}
Match Case must be ticked off, and Regular expression must be on.

I would run replace 3 times, once for each schema name:
Find:
create table Service\.
Replace with:
create table Svc.
Find:
create table Support\.
Replace with:
create table Spt.
Find:
create table Operation\.
Replace with:
create table Opn.
Or here is one that uses groups references:
Find:
Service(\.[^\s]+)(.*)
Replace with:
Svc\1\2
Here \1 will hold the dot operator and the table name and \2 holds the rest of the line.

Notepad++ regex implementation is not really powerfull; so,
other tools if they can?
Here is a way to do it:
perl -pi.back -e '%tr=(Service=>"Sev",Operation=>"Opn",Support=>"Spt");s/(?<=create table )(\w+)/$tr{$1}/e;' TheFile
You can add any number of Original => 'Modified' as you want within the hash %tr.
TheFile will be backuped into TheFile.back before processing.

Related

Regex query to remove character(s) from lines that has a phrase

I'm trying to convert my DDL's from Oracle to Postgres but I'm having a problem with double quote characters. I want to remove double quotes from each and every line which contains "CREATE TABLE " phrase. For example I want this: CREATE TABLE "ILIKEMEMES" to be converted to this: CREATE TABLE ILIKEMEMES but I don't want line ("ID" VARCHAR(255) to change either. I'm doing this on Notepad++ so Python scripts wouldn't be my first choice of solution.
Try doing the following find and replace, in regex mode:
Find: \bCREATE TABLE "(.*?)"
Replace: CREATE TABLE $1
This will target only create table statements having a table name which appears in double quotes.

How to combine multiple RegEx commands for Notepad++ using capture groups and alternations?

I am converting exported SQL views as files to a different syntax using a separate specialized conversion tool. This tool can't handle certain commands and formatting so I'm using Notepad++ with RegEx to alter the files ahead of time.
So far I am getting the results that I want, but it takes three separate Find/Replace actions. I'd like to reduce these three RegEx actions down to one if possible.
Find: (.*)(CREATE VIEW.*\nGO)(.*)
Replace: \2
Find: (CREATE VIEW )(.*)(\r\nAS)
Replace: \1"\2"\3
Find: (oldschema1\.|\[oldschema1\]\.|\[|\]|TOP \(100\) PERCENT|oldschema2\.)|(^GO$)|(\A^(.*?))
Replace: (?1)(?2\;)(?3SET SCHEMA schemaname\; \n\n\1)```
I'm using Notepad++ 7.7.1 64-bit, Find/Replace with Regular Expression search mode - ". matches newline" check on.
You'll see in my code that I'm already using capture groups with alternation. I thought I could combine the first two RegEx steps as additional capture groups to Step 3 but it doesn't work out, possibly because they are nested.
I tried referencing the nested groups by incrementing the referencing number accordingly, but it doesn't work (blanks out the result).
Here is an example SQL view file. It's not a working view because I added "oldschema2" so the RegEx would have something to find for one of the replacements, but it's representative as an example here.
garbage
text
beforehand
CREATE VIEW [oldschema1].[viewname]
AS
SELECT DISTINCT
TOP (100) PERCENT oldschema1.TABLENAME.FIELD1, oldschema1.TABLENAME.FIELD2
FROM oldschema1.TABLENAME
WHERE (oldschema1.TABLENAME.FIELD3 = N'Z003') AND oldschema2.TABLENAME.FIELD2 = 1
ORDER BY oldschema1.TABLENAME.FIELD1
GO
garbage
text
after
Here is some additional details of what I'm trying to achieve with each pass.
Notepad++ RegEx Step 1 - isolate view block from CREATE VIEW to GO
Find:
(.*)(CREATE VIEW.*\nGO)(.*)
Replace:
\2
Step 2 - put quotes around view name
Find:
(CREATE VIEW )(.*)(\r\nAS)
Replace:
\1"\2"\3
Step 3 - remove/replace various texts and insert a line at the beginning of the file
Find:
(oldschema1\.|\[oldschema1\]\.|\[|\]|TOP \(100\) PERCENT|oldschema2\.)|(^GO$)|(\A^(.*?))
Replace:
(?1)(?2\;)(?3SET SCHEMA schemaname\; \n\n\1)
The expected output from the above example would be:
SET SCHEMA schemaname;
CREATE VIEW "viewname"
AS
SELECT DISTINCT
TABLENAME.FIELD1, TABLENAME.FIELD2
FROM TABLENAME
WHERE (TABLENAME.FIELD3 = N'Z003') AND TABLENAME.FIELD2 = 1
ORDER BY TABLENAME.FIELD1
;
which I achieve with the above three steps, but I'd like to do it in one Find/Replace if possible.
I'm pretty new to RegEx, and StackOverflow for that matter. Your help is greatly appreciated.
Step 1
I'm not so sure about it, but I'm guessing that maybe we would want an expression similar to:
[\s\S]*?(CREATE VIEW[\s\S]*GO\s*)[\s\S]*
to be replaced with $1, where our desired data is in this capturing group:
(CREATE VIEW[\s\S]*GO\s*)
and we can even remove \s*:
(CREATE VIEW[\s\S]*GO)
and just try:
[\s\S]*?(CREATE VIEW[\s\S]*GO)[\s\S]*
with an m flag.
In the right panel of this demo, the expression is further explained, if you might be interested.
Step 2
We can likely try:
(CREATE VIEW)(.*)
and replace with:
SET SCHEMA schemaname;\n\n$1 "viewname"
Demo
Step 3
This step would probably be done with an expression similar to:
TOP \(100\) PERCENT |oldschema1\.
being replaced with an empty string.
Demo
Step 4:
\s*GO being replaced with \n; or just ; and we might likely have the desired output, not sure though.
Demo

Regex for custom GROUP BY statement

I'm trying to write a Java-compatible regex for custom GROUP BY statement to parse expressions like this:
GROUP BY table1.feild1, table2.feild2 UNDER table3
The idea is to get multiple "group by" tables somehow, along with a single "under" table.
I've tried something like this, but it does not work -
^\s*group\s*by\s*([,]*[\s]*([A-Za-z0-9_]+\.[A-Za-z0-9_]+){1,})\s{1,}under\s{1,}([A-Za-z0-9_]+)$
I'm not even sure that it can be done in a single regex. Maybe it should be split?
Try Regex: ^\s*group\s+by\s+([A-Za-z0-9_]+\.[A-Za-z0-9_]+(?:,\s*[A-Za-z0-9_]+\.[A-Za-z0-9_]+)*\s+)under\s+([A-Za-z0-9_]+)$
Demo

postgres substring regex multiple results

I'm trying to find image tags urls in a text field with multiple instances.
I'm currently using this code to extract the URL from the text field:
SUBSTRING(text_field FROM 'src="([^"]*).*')
The problem is it only returns the first instance of a image tag.
Is there a way to return multiple instances of matching from a single query?
Use the function regexp_matches() with the 'g' flag, example:
with my_table(text_field) as (
values ('src="first";src="second"')
)
select match[1] as result
from my_table
cross join lateral regexp_matches(text_field, 'src="([^"]*)', 'g') as match
result
--------
first
second
(2 rows)
Read about POSIX Regular Expressions in the documentation.

Replacing string using regex in notepad++

I have .txt file like this
The Catalog entry "33102490" - Catalog group "1293"
Stack trace:
com.ibm.commerce.catalog.dataload.exception.CatalogDataLoadApplicationException: The Catalog entry "33102490" - Catalog group "1293"
$1l.java:37)
at java.lang.reflect.Method.invoke(Method.java:611)
at com.ibm.ws.bootstrap.WSLauncher.main(WSLauncher.java:267)
I want only "33102490" and "1293" in the file. All other things need to be replaced.
Ctrl+H
Find what: ^.*Catalog entry ("\d+").*Catalog group ("\d+").*$
Replace with: $1\n$2
Replace
Make sure you have checked Regular expression and . matches newline