Vi, replace text - replace

I have text like this
template
template
template_results
template
And I need to replace it to this
template_form
template_form
template_results
template_form
How can I replace every match of template that is not followed by _ character in Vi?
I tried it like this
:%s/template[^_]/template_form - Pattern not found
:%s/template\[^_]/template_form - Pattern not found
:%s/template[_]/template_form - This works, but the pattern is opposite of what I need
Thank you :)

Use negative lookahead:
:%s/template\(_\)\#!/template_form/gc
This means match any "template" not followed (\#!) by any "_"
See: :help /zero-width

You're trying to specify template then nothing.
So the trick is to use the end of line specifier $
:%s/template$/template_form/g
The first one doesn't work because this [^_] matches any single character other than underline, but not no characters (or end of line, apparently).

This is easy in VIM, but sticking to what I BELIEVE to be in vanilla VI, you could do
:%s/template$/template_form/
This assumes that each line is followed by a end-of-line. If not, try:
:s/template\(\s\|$\)/template_form/
I don't know if the "magic" is right for your flavor of VI, but this matches "template" followed by whitespace or the end-of-line.

Related

Append End of Line with Substring from Current Line [duplicate]

This question already has an answer here:
Replace with whole match value using Notepad++ regex search and replace
(1 answer)
Closed 9 months ago.
I've scoured Stack Overflow for something just like this and can't seem to come up with a solution. I've got some text that looks like this:
command.Parameters.Add("#Id
command.Parameters.Add("#IsDeleted
command.Parameters.Add("#MasterRecordId
command.Parameters.Add("#Name
...
And I would like the text to end up like this:
command.Parameters.Add("#Id", acct.Id);
command.Parameters.Add("#IsDeleted", acct.IsDeleted);
command.Parameters.Add("#MasterRecordId", acct.MasterRecordId);
command.Parameters.Add("#Name", acct.Name);
...
As you can see, I essentially want to append the end of the line with: ", acct.<word between # and second ">);
I'm trying this:
Find What: (?<=#).+?(?=\r) - This works, it finds the appropriate word.
Replace: \1", acct.\1); - This doesn't. It changes the line to (for Id):
command.Parameters.Add("#", acct.
Not sure what I'm doing wrong. I thought that \1 is supposed to be the "capture" from the "Find what" box, but it's not I guess?
The \1 backreference will only work if you have a capturing group in your pattern:
(?<=#)(.+?)(?=\r)
If you're not using a capturing group, you should use $& instead of \1 as a backreference for the entire match. Additionally, parentheses in the replacement string need to be escaped. So, the replacement string should be:
$&", acct.$&\);
You might also want to use $ instead of the Lookahead (?=\r) in case the last line isn't followed by an EOL character.
Having said all that, I personally prefer to be more explicit/strict when doing regex substitution to avoid messing up other lines (i.e., false positives). So I would go with something like this:
Find: (\bcommand\.Parameters\.Add\("#)(\w+)$
Replace: \1\2", acct.\2\);
Note that \w will only match word characters, which is likely the desired behavior here. Feel free to replace it with a character class if you think your identifiers might have other characters.
You could also omit the lookbehind, and match the # and then use \K to clear the current match buffer.
Then you can match the rest of the line using .+
Note that you don't have to make the quantifier non greedy .*? as you are matching the rest of the line.
In the replacement, use the full match using $0
See a regex demo for the matches:
Find what:
#\K.+
Replace with:
$0", acct.$0\)
If there must be a newline to the right, you might also write the pattern as one of:
#\K.+(?=\r)
#\K.+(?=\R)

Cut lines using Notepad++ Regexp replace

I need to cut lines that have 6 or more characters, hyphen, then other characters or symbols. Hyphen and rest of line should be removed. Source text:
0402CS-2
0402CS-3
0402
7812-C
0603CS-1
0603CS-2
0603CS-3
As a result, I need this:
0402CS
0402CS
0402
7812-C
0603CS
0603CS
0603CS
To do that, I use Notepad++ regexp replace feature. Find pattern: ^([^\-]{6,})\-.+$ Replace pattern: \1
But there is no option "multiline", so, symbols "^" and "$" doesn't match ONLY beginning and end of the line and actually I have result:
0402CS
0402CS
0402
7812 <-- that's wrong!
0603CS
0603CS
0603CS
Please advice me how to fix find pattern? Or, maybe there is other handful and powerful free text editor that can do that?
^([^\n\-]{6,})\-.+$
^^
Just use \n as due to [^-] the regex can traverse to line below as use that line to make a match.
See demo.
https://regex101.com/r/BHO93c/1
for the input
0402
7812-C the regex matches both lines as 1 line and makes a match.
See demo if 0402 is not there.
https://regex101.com/r/BHO93c/2
That happens because the [^-] character class also matches a newline.
Add \n to it:
^([^\n-]{6,})-.+$
See the regex online demo (note the m multiline modifier (making ^ match the start of the line, and $ - the end of the line) and g modifier (enabling search for multiple occurrences) that is ON by default in Notepad++).
Note that escaping the hyphen is not necessary inside a character class when it is at the start/end of the class, and you never need to escape the hyphen outside the character class.

RegExp to match visible non-letter characters before line break

I am working on a vbs regexp that will detect a tag which contains text and a CRLF character before closing tag.
I am currently using \w+[:;?!.,""\)\]-~]*(\s)*(\r\n\s*)(<\/.*>)
Looking from the end of the expression, I am matching any closing tag, CRLF plus optionally blank spaces, an optional spaces before CRLF and it should optionally match any other visible non-letter character which occurs after any word.
This is to match things like
myword! CRLF</tag>
mywordCRLF</tag>
myword CRLF</tag>
myword...CRLF </tag>
etc.
However, I do not want to match below, as I need to detect tags containing TEXT and linebreaks.
</otherclosingtag> CRLF </tag>
I am concerned about the \w+[:;?!.,""\)\]-~]* bit as it doesn't look right to me, as I would need to insert quite a large number of characters here.
I tried replacing it with \S, \W but they all seem to match CRLF characters as well.
Any ideas?
Cheers!
How about using non-greedy modifier:
\w+\W*?\r\n\s*(<\/.*>)
or
\w+[^\r\n]*\r\n\s*(<\/.*>)
The solution that I used:
\w+[^\r\n<>]*(\r\n\s*)(<\/.*>)
It matches a word (so not ) then anything that is not the CR, LF or > (so it doesn't match openingtag> CRLF</closingtag>)
This is a modified version of what M42 has proposed, I had added <> to make sure we won't match a tag.
Thanks for suggestions!
Try this:
^.*[\n\t\s]*</.*>$ --> BAD
^.*[\r\n\t\s]*</.*>$

Vim regex removing spaces before the end of line

I have a file, that contains spaces at the end of the line and they should be removed.
When I use the following command:
%s/\s+$//
Vim shows me an error that pattern is not found. What is wrong here?
NOTE: actually, I can use the %s/\s*$// command but I want to understand the root cause of the issue.
Vim shows me an error that pattern is not found
You need to escape the quantifier +.
:%s/\s\+$//g
You might also want to refer to Quantifiers, Greedy and Non-Greedy.
You can use it like this:
%s/ \+$//
OR:
%s/\s\+$//
As + needs to be escaped in vim regex.
Using the "very magic" mode with \v, you only need to escape alphanumeric specials:
%s:\v\s+%(//.*)?$::
(Here : is used as a separator, instead of /. You can use almost any non-alphanumeric ASCII character.)
another way to do it:
:%s/ *$//g
if you do not want to input '\' chars ;)

Notepad++ Replace all with an exception

I am attempting to edit a csv file, below is a sample line from this file.
|MIGRATE|;|10000|;|2ACC0003|;|30/09/13|;|Positive Adjmt.|;||;|MIGRATE|;|95004U
The beginning of the line |MIGRATE| needs to be modified without changing the second MIGRATE so the line would read
|MIGRATE|;|MIG_IN|;|10000|;|2ACC0003|;|30/09/13|;|Positive Adjmt.|;||;|MIGRATE|;|95004U
There are 7700 or so lines so if I am forced to do this manually I will probably cry a little.
Thanks in advance!
Just replace all the ones you want not changed with another word temporarily, then replace the rest with what you want. I'm not sure what you're asking here, but from what I can guess this might help.
It seems like you could just search for Just search for:
^\|MIGRATE\|
And replace with:
|MIGRATE|;|MIG_IN|
Make sure you've checked 'Regular expression' in the 'Search Mode' options.
Explanation: The ^ is a begin anchor; it will match the beginning of the line, ensuring that it does not match the second |MIGRATE|. The \ characters are required to escape the | characters since they normally have special meaning in regular expressions, and you want to match a literal |.
You can use beginning of line anchors:
Find:
^(\|MIGRATE\|)
Replace with:
$1;|MIG_IN|
regex101 demo
Just make sure that you are using the regular expression mode of the Search&Replace.
If you want to be a bit fancier, you can use a positive lookbehind:
Find:
(?<=^\|MIGRATE\|)
Replace with:
;|MIG_IN|
^ Will match only at the beginning of a line.
( ... ) is called a capture group, and will save the contents of the match in variable you can use (in the first regex, I accessed the variable using $1 in the replace. The first capture gets stored to $1, the second to $2, etc.)
| is a special character meaning 'or' in regex (to match a character or group of characters or another, e.g. a|b matches a or b. As such, you need to escape it with a backslash to make a regex match a literal |.
In my second regex, I used (?<= ... ) which is called a positive lookbehind. It makes sure that the part to be matched has what's inside before it. For instance, (?<=a)b matches a b only if it has an a before it. So that the b in ab matches but not in bb.
The website I linked also explains the details of the regex and you can try out some regex yourself!