regex replacing at beginning of line in notepad++ - regex

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.

Related

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

Remove columns from CSV

I don't know anything about Notepad++ Regex.
This is the data I have in my CSV:
6454345|User1-2ds3|62562012032|324|148|9c1fe63ccd3ab234892beaf71f022be2e06b6cd1
3305611|User2-42g563dgsdbf|22023001345|0|0|c36dedfa12634e33ca8bc0ef4703c92b73d9c433
8749412|User3-9|xgs|f|98906504456|1534|51564|411b0fdf54fe29745897288c6ad699f7be30f389
How can I use a Regex to remove the 5th and 6th column? The numbers in the 5th and 6th column are variable in length.
Another problem is the User row can also contain a |, to make it even worse.
I can use a macro to fix this, but the file is a few millions lines long.
This is the final result I want to achieve:
6454345|User1-2ds3|62562012032|9c1fe63ccd3ab234892beaf71f022be2e06b6cd1
3305611|User2-42g563dgsdbf|22023001345|c36dedfa12634e33ca8bc0ef4703c92b73d9c433
8749412|User3-9|xgs|f|98906504456|411b0fdf54fe29745897288c6ad699f7be30f389
I am open for suggestions on how to do this with another program, command line utility, either Linux or Windows.
Match \|[^|]+\|[^|]+(\|[^|]+$)
Repalce $1
Basically, Anchor to the end of the line, and remove columns [-1] and [-2] (I assume columns can't be empty. Replace + with * if they can)
If you need finer detail then that, I'd recommend writing a Java or Python script to manual parse and rewrite the file for you.
I've captured three groups and given them names. If you use a replace utility like sed or vimregex, you can replace remove with nothing. Or you can use a programming language to concatenate keep_before and keep_after for the desired result.
^(?<keep_before>(?:[^|]+\|){3})(?<remove>(?:[^|]+\|){2})(?<keep_after>.*)$
You may have to remove the group namings and use \1 etc. instead, depending on what environment you use.
Demo
From Notepad++ hit ctrl + h then enter the following in the dialog:
Find what: \|\d+\|\d+(\|[0-9a-z]+)$
Replace with: $1
Search mode: Regular Expression
Click replace and done.
Regex Explain:
\|\d+ : match 1st string that starts with | followed by number
\|\d+ : match 2nd string that starts with | followed by number
(\|[0-9a-z]+): match and capture the string after the 2nd number.
$ : This is will force regex search to match the end of the string.
Replacement:
$1 : replace the found string with whatever we have between the captured group which is whatever we have between the parentheses (\|[0-9a-z]+)

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\*\'$/\\/

notepad++ regex replace word in first line

Im trying to use the following regex to search and replace in multiple files in notepad++
([^\n]*)(state="1")([^\n]*)*.
This searches and finds state="1" in the first line of each file and works fine.
However, when I try to replace state="1" using:
Replace with: $1 state="5"
it cuts off the rest of the line.
I thought that it might be possible to get the rest of the line using:
Replace with: $1 state="5" $2
However, $2 doesnt seem to exist as a variable.
Is there some way to attach the rest of the line into variable $2?
Cheers
Heres an image to show how
(?=\A[^\n]*)state="1"
is not working
Ive updated my version of notepad++ and everything
Each capture group, (…), is assigned a number, so $2 represents the second capture group, (state="1"). The remainder of the line is captured in $3.
Either remove the capture group around state="1":
([^\n]*)state="1"([^\n]*)*.
Or use $3:
Replace with: $1 state="5" $3
Also, given the simplicity of the task, I don't see why you couldn't just search for state="1" and replace with state="5". There doesn't seem to be any need for regular expressions here.
Update There's nothing in the pattern listed so far which limits the result to only matching strings on the first line. If you need that I'd recommend using a pattern like this:
(?=\A[^\n]*)state="1"
With these settings:
Update There seems to be some strange behavior with the \A (beginning of text) anchor inside the lookbehind. Removing from the lookbehind seems to work. Try this pattern:
\A([^\n]*)state="1"
And replace with:
$1state="5"
All the other settings should be fine.

Numbers & escape characters for regex replacement in Notepad++

I'm trying to match a pattern using RegEx in notepad++, but not having much luck. I'm able to match part but not all of it.
I need to search for this line:
<size value="Large" pax="13074"/>
And replace it with this:
<size value="Very_large" pax="41450" cargo="Largest" cargovolume="3227"/>
Essentially I need to find all patterns matching pax="n"/> and replace them with pax="n" cargo="Largest" cargovolume="0"/> while retaining the initial value of n.
So, ideas anyone?
Press Ctrl + F, move to tab Replace, in Find what do: pax="(\d+)" and in Replace with put this: pax="\1" cargo="Largest" cargovolume="0"
Remember to mark regex. That should retain the number and replace the content.
UPDATE: Hint about saving text for replacement.
Whenever you use regex to do text replacement, wrap the content you want to save in parenthesis and then you can access them using \i where i is the order of appearance of the parenthesis starting at 1.
Hope it helps!