Notepad++ can find but can not replace a string - regex

I have a string like this:
INSERT INTO `test` VALUES (999,'stuff',NULL,'2014-12-01 08:09:10');
What I want is remove some string to get the value between single quotes:
stuff
I've used 2 regex:
^.*\d,'
and
',NULL.*$
When I use count in Npp, it returns Count: 1 match., but when I use replace, it returns Replace: no occurrence was found.
Do you have any idea?

You just need to combine both regexes using | OR operator. Finally in the replacement part, you need to give an empty string, so that it would remove all the matched characters.
^.*\d,'|'.*$
OR
^.*\d,'|',NULL.*$
DEMO
OR
Find What: ^.*\d,'([^']*)',NULL.*$
Replace With : \1
DEMO

Related

Notepad++ Replace function

Can somehow automatically replace with this outcome:
INITIAL:
"eye","doc":441,"engagement":35877,"impression":1987869,"reach":294757,"repost":441}}
OUTCOME:
{"emojiAnnotations":{"eye","doc":441,"engagement":35877,"impression":1987869,"reach":294757,"repost":441}}
When "eye" and so on are variables....(only titles are the same)??
I thought to use - something like - "*","doc" replace (as an expression) with {"emojiAnnotations":"*","doc", but this will not keep variable "eye" and so on.
P.S. This is part of replacement process so to add at the beginning is not acceptable
Is this OK for you?
Ctrl+H
Find what: "[^"]+","doc"
Replace with: {"emojiAnnotations":{$0
check Wrap around
check Regular expression
Replace all
Explanation:
"[^"]+" : 1 or more not quote beweeen quotes
,"doc" : literally
Replacement:
{"emojiAnnotations":{ : literally
$0 : the whole match
Result for given example:
{"emojiAnnotations":{"eye","doc":441,"engagement":35877,"impression":1987869,"reach":294757,"repost":441}}

Finding the string or substring within nth occurrence in a line

I would like to find the third occurrence of string inside quotes in order to replace this string or part of this string. This is a typical line I have to deal with:
"/activities/absenceactivity/lang/#LANG_ID#/prop_dialog.php";"BA2_PD_SCR";"Opis
dogodka";"Event description";"Descrição do Evento";"ÐпиÑ
подÑÑ";"";"č®vykio aprašymas";"Descripción del evento";"";
I know that "([^"]*)" shows every occurrence of text and quotes but I would like to get just the third one, in this example "Opis dogodka" in order to perform Search & Replace in Sublime Text.
Problem is to find the third occurrence of string within the quotes, replace it entirely or just partially and make sure that the Regex provides also a solution for an empty
""
strings.
Thank you.
I'm sure there are ways to simplify this further, but if you're ok with brute force:
Sublime command:
Find: "[^"]*";"[^"]*";"([^"]*)".*
Replace: $1
NP++:
Find what: "([^"]*)";"([^"]*)";"([^"]*)".*
Replace with: $3
sed:
sed 's/"\([^"]*\)";"\([^"]*\)";"\([^"]*\)".*/\3/'
You can use {} pattern repetition:
/(?:"([^"]*)";){3}/ # first match group will be 'Opis dogodka'
Demo
Or, use a global type match and then take the third match. This might require logic such as slicing or looping:
/"([^"]*)";/g
Demo 2
Or, just manually put in the first two patterns to skip:
/^"[^"]*";"[^"]*";("[^"]*";)/
Demo 3
Or combine repetition to skip the first n-1 then capture the nth match:
/^(?:"[^"]*";){2}("[^"]*";)/
Demo 4

Multi-line search & replace for Beginning and End of Each Line in Notepad++

To start off, I want to be able to do 2 things:
1st Thing:
To extract foo_abc (and similarly every other line, for example, goo_zxy, and doo_fgh), I needed to remove some text appended BEFORE foo_abc, and AFTER foo_abc.
For example:
TEXTBEFOREfoo_abcTEXTAFTER
TEXTBEFOREgoo_zxyTEXTAFTER
TEXTBEFOREdoo_fghTEXTAFTER
to obtain:
foo_abc
goo_zxy
doo_fgh
2nd Thing:
I now need to append different text before and after foo_abc again.
Like so:
TextAfoo_abcTextB
So what I've done is:
Find: ^
Replace: TextA
Find: $
Replace: TextB
Which works well, but I have to perform a find&replace TWICE which is not very efficient. To avoid that, I found this: Multiple word search and replace in notepad++
And applied it like so:
Find: (^)|($)
Replace: (?1TextA)(?2TextB)
But it doesn't work out too well.
AND, as mentioned, I need this to work for EACH and every line:
For example:
foo_abc
goo_zxy
doo_fgh
I need to insert TextA at the beginning for each of those lines, and TextB at the end of each line, like so:
TextAfoo_abcTextB
TextAgoo_zxyTextB
TextAdoo_fghTextB
Can this be done? (Yes, I actually need to do this to over 10000 lines, not just 3 and wanting an efficient way to do so).
Have I missed a quicker way to do all of this? Perhaps by performing a search and replace above in '1st Thing' on the TEXTBEFORE and TEXTAFTER, with TextA and TextB, respectively, in one-go?
Many thanks.
EDIT: Yes, they are literal strings. Yes, they do contain special characters because they are represent parts of a URL.
There are two scenarios: 1) you want to replace the TEXTBEFORE or TEXTAFTER regardless of the fact that either of them exists, 2) both TEXTBEFORE and TEXTAFTER must exist
Scenario 1
You may use a single search and replace operation for this:
Find What: ^(TEXTBEFORE)|TEXTAFTER$
Replace With: (?{1}TextA:TextB)
NOTE: If the TEXTBEFORE and TEXTAFTER contain special chars, you may use
Find What: ^(\QTEXTBEFORE\E)|\QTEXTAFTER\E$
Details:
^(TEXTBEFORE)- match and capture into Group 1 TEXTBEFORE at the start of a line
| - or
TEXTAFTER$ - match TEXTAFTER at the end of a line.
Replacement pattern:
(?{1} - if Group 1 is matched, then
TextA - return TextA
: - else
TextB - replace with TextB
) - end of the conditional replacement pattern.
Scenario 2
If you need to match lines starting with some text and ending with another, use
Find What: ^TEXTBEFORE(.*?)TEXTAFTER$
Replace With: TextA$1TextB
Details:
^ - start of a line
TEXTBEFORE - some text here
(.*?) - Group 1 (that can be referred to with $1 backreference from the replacement pattern) matching any 0+ chars other than line break chars
TEXTAFTER - some text at the...
$ - end of line.
Try:
TEXTBEFORE(.+?)TEXTAFTER
replace with
TextA$1TextB
See this for example and explanation
If you need to find whole line:
^TEXTBEFORE(.+?)TEXTAFTER$
Replace is the same as before.

regex replacing at beginning of line in notepad++

for example I have txt with content
qqqqaa
qqss
ss00
I want to replace only one q at the beginning of line, that is to get
qqqaa
qss
ss00
I tried replace ^q in notepad++. But After I click replaceAll, I got
aa
ss
ss00
What is wrong? Is my regex wrong? What is the correct form?
The issue is that Notepad++ Replace All functionality replaces in a loop using the modified document.
The solution is to actually consume what we need to replace and keep within one regex expression like
^q(q*)
and replace with $1.
The pattern will find a q at the beginning of the line and then will capture into Group 1 zero or more occurrences of q after the first q, and in the replacement part the $1 will insert these qs inside Group 1 back into the string.
You can use ^q(.+) and replace with $1 if you also want to replace single q's.

VIM - Replace based on a search regex

I've got a file with several (1000+) records like :
lbc3.*'
ssa2.*'
lie1.*'
sld0.*'
ssdasd.*'
I can find them all by :
/s[w|l].*[0-9].*$
What i want to do is to replace the final part of each pattern found with \.*'
I can't do :%s//s[w|l].*[0-9].*$/\\\\\.\*' because it'll replace all the string, and what i need is only replace the end of it from
.'
to
\.'
So the file output is llike :
lbc3\\.*'
ssa2\\.*'
lie1\\.*'
sld0\\.*'
ssdasd\\.*'
Thanks.
In general, the solution is to use a capture. Put \(...\) around the part of the regex that matches what you want to keep, and use \1 to include whatever matched that part of the regex in the replacement string:
s/\(s[w|l].*[0-9].*\)\.\*'$/\1\\.*'/
Since you're really just inserting a backslash between two strings that you aren't changing, you could use a second set of parens and \2 for the second one:
s/\(s[w|l].*[0-9].*\)\(\.\*'\)$/\1\\\2/
Alternatively, you could use \zs and \ze to delimit just the part of the string you want to replace:
s/s[w|l].*p0-9].*\zs\ze\*\'$/\\/