Searching multiple tags with regex - vim

I am trying to search for multiple tags with regex.
Something like this:
:tag /blabla\|user
I would like to search tags for blabla and user but for some reason it is not working. I tried all the combination I can think of, with and without magic flag. The help file said it accepts regex so I think it should be working.

Your original command, :tag /blabla\|user, is interpreted "as-is"; it needs one more forward slash to work:
:tag /blabla\\|user

Related

Is there a quick way to delete everything between certain tags, e.g. <head> and </head>, throughout the whole project (multiple pages) in VS Code?

I am trying to find a way to remove all from a tag pair in VS Code.
I’ve been using Notepad++ for this purpose, but for some unknown reason it doesn't work all the time. So, I hope if there is such a possibility in VS Code, it’d be more reliable.
Here is the instruction for Notepad++:
Search for -
<wp:post_name>[^<>]+</wp:post_name>
and replace all with -
<wp:post_name></wp:post_name>
Is there anything like this in VS Code?
I’d really appreciate it if someone can help.
Before using what is suggested in this solution, backup your files, and run the search and replace on a small sample. Be sure to check the outcome to all the possible combinations you can have in your files.
You can achieve what you need with Notepad++ (and SublimeText 3, with RegEx search and replace), and this answer will cover that. Since I've never used Visual Studio Code, I can't say if it will work in it as well.
Consider the following regular expression.
<foo>(.*?)<\/foo>
If we were to apply it to the following text:
<foo><some special chars>!##$%^&*</foo> sure, why not
<foo>Lorem</foo>
<foo>ipsum</foo>
<foo>sit</foo>
<foo>dolor</foo>
<foo>amet</foo>
<bar>elm stuff</bar>
more stuff for you <foo> something </foo> and even more stuff <foo>yes</foo>
it would match all the parts of the text which begin with <foo> and end with </foo>, regardless of what's between them.
If you want to play around with this, I've created an example here.
As far as using this in Notepad++, open the search window, navigate to the Find in files tab, and set it up like in the following image.
You would, of course, need to change the search and replacement strings to those you plan on using, optionally set up a file extension for which to do the replacement (Filters), and set the directory in which to perform find-and-replace.
Limitations
1. Nesting
In case your text contains nested tags of the same kind, like this:
Let's deal with nesting: <foo> some text <foo> a child foo!</foo> let's close the parent</foo>
doing the suggested RegEx search and replace, will turn the previous line of text into this:
Let's deal with nesting: <foo></foo> let's close the parent</foo>
If you don't have nested tags of the same kind, you should be in the clear. Unless...
2. Newlines
The provided RegEx will not match cases where your opening tag shows up in one line, and the closing tag shows up in another line. To match those, you would need to change the original RegEx:
<foo>(.*?)<\/foo>
to this:
<foo>([\s\S]*?)<\/foo>
\s will match any whitespace character (including newlines), while \S will match any non-whitespace character.

How to change var[foo] to foo using Vim substitution?

Today I came across this situation where I need to change var[name] to var [name] and a lot of other place where name could be description or other string. Can I do something like the following command:
%s/var[$variable]/$variable/g
Based on the title of the question, the following expression should change var[foo] to foo:
:%s/\vvar\[(.{-})\]/\1/g
But in the details you mentioned that you need to change var[name] to var [name], following should do that:
:%s/\v(var)(\[.{-}\])/\1 \2/g
Other ways of doing the second substitution from comments:
:%s/var\zs\ze\[.*\]/ /g
or
:%s/var\zs\ze\[[^]]*\]/ /g
Related: (g)vim replace regex
The thing to know, if you already know about regex and captures in general, is in this context you apparently have to escape the group characters, rather than it being the other way around:
%s/var[\(variable\)]/\1/g
Without more info, I can't be more helpful.

Vi search and replace with slashes

I am trying to run this command in vi
:s/href="\//href="http:\/\/website.com\/folder\/subfolder\//g
but got this error E486: Pattern not found: href="\/
What am i doing wrong?
That error means pretty much what it says. vi didn't find any pattern href="/ (ignoring escapes) in your file.
Sometimes it's easier to use something besides / for the search delimiter if your search has a lot of slashes, so you don't need to escape them all. Try replacing the / delimiter with # instead, like this:
s#href="/#href="http://website.com/folder/subfolder/#g
Then maybe you can more easily see what's wrong with your pattern:
becouse there are many '/' chars, try use another delimiter, ex ',':
:s,some/pattern/with/slashes,new/string,g
On another note. That substition worked for me. Just copied and pasted. Are you on the same line that you are trying to perform the substitution on? the 'g' is meant globally on the line you are on. If you need to perform the search and replace on the file the use :%s/

vim does not find and replace simple phrase that is clearly present

I have a simple vim problem that Google hasn't managed to help me with. Any thoughts are appreciated.
I do the following search and replace:
:s/numnodes/numnodes1/g
On a file containing the following text:
numprocs=0
numnodes=0
I get
E486: Pattern not found
The position of the green square which indicates where I'd start typing is clearly above the pattern. I tried searching for other short phrases not involving regex, which are also present, which also fail. A simple /numnodes highlights matches as expected. Does anyone have any idea what might be the matter with vim?
Try :%s/searchphrase/replacephase/g
Without the % symbol Vim only matches and replaces on the current line.
try using this:
:%s/numnodes/numnodes1/g

How to delete text in a file based on regular expression using vim

I have an XML file like this:
<fruit><apple>100</apple><banana>200</banana></fruit>
<fruit><apple>150</apple><banana>250</banana></fruit>
Now I want delete all the text in the file except the words in tag apple. That is, the file should contain:
100
150
How can I achive this?
:%s/.*apple>\(.*\)<\/apple.*/\1/
That should do what you need. Worked for me.
Basically just grabbing everything up to and including the tag, then backreferences everything between the apple begin and end tag, and matches to the rest of the line. Replaces it with the first backreference, which was the stuff between the apple tags.
I personally use this:
%s;.*<apple>\(\d*\)</apple>.*;\1;
Since the text contain '/' which is the default seperator,and by using ';' as sep makes the code clearer.
And I found that non-greedy match #Conspicuous Compiler mentioned should be
\{-}
instead of "{-}" in Vim.
However, I after change Conspicuous' solution to
%s/.*apple>(.\{-\})<\/apple.*/\1^M/g
my Vim said it can't find the pattern.
In this case, one can use the general technique for collecting pattern matches
explained in my answer to the question "How to extract regex matches
using Vim".
In order to collect and store all of the matches in a list, run the Ex command
:let t=[] | %s/<apple>\(.\{-}\)<\/apple>\zs/\=add(t,submatch(1))[1:0]/g
The command purposely does not change the buffer's contents, only collects the
matched text. To set the contents of the current buffer to the
newline-separated list of matches, use the command
:0pu=t | +,$d_

Resources