How to replace a pattern in notepad++ - regex

I have a sql procedure code. We are migrating the code on different schema. I need to replace all the dimension tables schema.
Example:
Old schemas: DBO.ABC_DIM, DBO.XYZ_DIM
After replace: MART.ABC_DIM, MART.XYZ_DIM
Could any one let me know how we can do this using regex replace.
Thanks
Sky

You must use:
in the "Find what" field:
(DBO)\.
and in the "Replace with" field:
MART\.
Don't forget to place the cursor at beginning of the file. Otherwise the replacements begin after actually cursor position
EDITED:
So in this case if you have others, you can use that:
Find field:
\b(DBO\.)(.+?)_DIM\b
Replace field:
MART\.$2_DIM
Some like:
DBO.ABC_DIM, DBO.XYZ_DIM,
DBO.ABC_DTL, DBO.ABC_2_BCD
become:
MART.ABC_DIM, MART.XYZ_DIM,
DBO.ABC_DTL, DBO.ABC_2_BCD
LAST EDIT:
The above fail with:
DBO.ABC_DIM, DBO.XYZ_DIM,
DBO.ABC_DTL, DBO.ABC_2_BCD, DBO.ABC_DIM, DBO.XYZ_DIM,
DBO.ABC_DTL, DBO.ABC_2_BCD,
DBO.ABC_DIM, DBO.XYZ_DIM,
Because in the second row match DBO.ABC_DTL, DBO.ABC_2_BCD, DBO.ABC_DIM
And DBO.ABC_DTL become MART.ABC_DTL
So the right solution is:
Find field:
(DBO\.)(.[^\.]+?)_DIM
Replace field:
MART\.$2_DIM
see matching results here: http://refiddle.com/refiddles/596b348175622d74ff020000

if you open that schema in VIM, do press esc and then
:s%/DBO/MART
and press enter
:s (colon and s) for substitute
/DBO find DBO
/MART replace it with MART
once you verify that all the DBOs are replace with MART, you need to save the changes by esc and :wq

Related

Sublime Regex Search and Replace

Trying to use Sublime to update the urls of only some lines in a sql table dump.
in this case the line that I need to single out has the string 'themo_showcase_\d_image' which is easy to match. In the same string what I actually need to replace is the url column so that it reads 'https://www.example.com/' to 'http://www.example.com'
Anyone able to help shed some light on this? I've got thousands of these insert records that I need to modify.
ex:
original string:
('8630', '1328', 'themo_showcase_1_image', 'https://www.example.com/'),
to:
('8630', '1328', 'themo_showcase_1_image', 'http://www.example.com/'),
Find: 'themo_showcase_\d_image', 'http\Ks you could use \d+ if there are more than 1 digit
Replace: LEAVE EMPTY

Postgres: remove second occurrence of a string

I tried to fix bad data in postgres DB where photo tags are appended twice.
The trip is wonderful.<photo=2-1-1601981-7-1.jpg><photo=2-1-1601981-5-2.jpg>We enjoyed it very much.<photo=2-1-1601981-5-2.jpg><photo=2-1-1601981-7-1.jpg>
As you can see in the string, photo tags were added already, but they were appended to the text again. I want to remove the second occurrence: . The first occurrence has certain order and I want to keep them.
I wrote a function that could construct a regex pattern:
CREATE OR REPLACE FUNCTION dd_trip_photo_tags(tagId int) RETURNS text
LANGUAGE sql IMMUTABLE
AS $$
SELECT string_agg(concat('<photo=',media_name,'>.*?(<photo=',media_name,'>)'),'|') FROM t_ddtrip_media WHERE tag_id=tagId $$;
This captures the second occurrence of a certain photo tag.
Then, I use regex_replace to replace the second occurrence:
update t_ddtrip_content set content = regexp_replace(content,dd_trip_photo_tags(332761),'') from t_ddtrip_content where tag_id=332761;
Yet, it would remove all matched tags. I looked up online for days but still couldn't figure out a way to fix this. Appreciate any help.
This Should Work.
Regex 1:
<photo=.+?>
See: https://regex101.com/r/thHmlq/1
Regex 2:
<.+?>
See: https://regex101.com/r/thHmlq/2
Input:
The trip is wonderful.<photo=2-1-1601981-7-1.jpg><photo=2-1-1601981-5-2.jpg>We enjoyed it very much.<photo=2-1-1601981-5-2.jpg><photo=2-1-1601981-7-1.jpg>
Output:
<photo=2-1-1601981-7-1.jpg>
<photo=2-1-1601981-5-2.jpg>
<photo=2-1-1601981-5-2.jpg>
<photo=2-1-1601981-7-1.jpg>

copy text between two semicolons and add it to the end of the line

I have text like this:
0;Anguilla;
0;Antarctica;
0;Antigua And Barbuda;
0;Argentina;
0;Armenia;
just like 300 Countries more...
I want to copy the Country Name between the two semicolons and add it to the end of the particular Line.
So it looks like this
0;Anguilla;Anguilla
0;Antarctica;Antarctica
0;Antigua And Barbuda;Antigua And Barbuda
0;Argentina;Argentina
0;Armenia;Armenia
I tried something like this
/;.*?;/
but that doesn't seem to work.
You are near to the solution, all you need is a capturing group:
search: ;(.*?);
replace: $0$1
In Notepad++ you can do this with Ctrl+H
Find What : (0;(.+?);)
Replace With : \1\2
Click Replace all. Make sure you have 'Regular Expression' Selected

Notepad++: Multiple line search & multiple line replace

I'm having a battle with a regex. (MOBI creation)
I have two files: one with XML, the other an HTML table of contents.
The important parts of the XML:
<navPoint id="_NeedsHTMLid" playOrder="40">
<navLabel><text>Needs anchor text from link.)</text></navLabel>
...
The HTML TOC, of course, looks like:
schema.org Article Mark-up
======
Hours and hours... worked with Textpad forever. Saw remarks here, now I'm using NotePad++... some of the regex results are different (NOT that I had it working anyway.) #_[\b(\w\b] was returning the ID: now? Not so much!
Does anyone know how to yank both the ID and the anchor text out of these? I'd be so grateful.
You can use this to get the id and the anchor text at the same time:
_(\w+)\b|([a-Z\s.]+[)]+)
#_[\b(\w\b] is not a valid regex. Try _([^"]+)\b.
Edited: try [^"] in place of \w.
If you want to match the ids and the text, go to Search > Find menu (shortcut CTRL+F) and do the following:
Find what:
id="([a-zA-Z0-9\-\:\_\.]+)"|<text>(.+?)<\/text>
Select radio button "Regular Expression"
Then press Find All in Current Document
You can test it with your example at regex101.
Here's a StackOverflow post about valid id names.
I didn't provided you with a Search and Replace solution, since you didn't mentioned anything about a replacement.

Need help with regex Email in Notepad++

I have a list with contacts each line, we have to replace the whole line in to single email:
Name, Surname, Address, Email, Phone
=>
Email
I know how to find email, but I need smth like find and replace to "" everything but Email
This worked for me using Notepad++ to remove everything except for the email addresses:
Ctrl + H to bring up Find/Replace dialog box.
Change to the the Replace tab.
Find what: ^.*(\<[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z][A-Za-z][A-Za-z]?[A-Za-z]?\>).*$
Replace with: \1
You need to select [Regular Expression] at the bottom of the Find/Replace dialog box.
Then click [Replace All]
Assuming your email regular expression is well-written and won't match anything that isn't an email...
Find (() characters are significant):
^.*(your email regex here).*$
Replace with:
\1
I don't think you can replace "everything except" any regex in notepad++. I usually use macros for such a problem.
But another method would it be, to import the data into Excel as a CSV, mark the column with the email adresses and copy-paste them to notepad++. That's another trick I usually do.