Search and replace with wildcard text in Notepad++ - search

I'm using Notepad++ and I'm trying to remove a number of lines from a file with the following example text:
<title lang="en"><a href=\"/movies/randomtext1/\">randomtext1</title>
<title lang="en"><a href=\"/movies/randomtext2/\">randomtext2</title>
I'd like to remove/replace the <a href=\"/movies/randomtext1/\"> section and just leave this on each line <title lang="en">randomtext1</title>
I'm just not sure now to enter the "wildcard" expression for the random text bit.
Hope that makes sense :-)

How about:
Find what: <.+?>
Replace with: NOTHING

Try a regular expression search/replace:
Find what: \<a href=\\"/movies/(.*)/\\"\>\1
Here we need to escape several characters that have a special meaning for regular expressions: & \ . The wildcard is the (.*), which means any (.) character repeated zero or more times (*). Because of the parentheses we can reference that part of the search with \1 in the search itself and in the repacement, too.
Instead of .* inside the parentheses you might also try [^/]* (everything which is not a slash).
Replace with: \1
Check Regular Expression in the lower left.

Related

Find and replace '\' in vim [duplicate]

For instance, if I wanted to a find and replace with strings containing backward or forward slashes, how would this be accomplished in vim?
Examples
Find & Replace is: :%s/foo/bar/g
what if I wanted to find all occurrences of <dog/> and replace it with <cat\>
Same way you escape characters most anywhere else in linuxy programs, with a backslash:
:%s/<dog\/>/<cat\\>
But note that you can select a different delimiter instead:
:%s#<doc/>#<cat\\>#
This saves you all typing all those time-consuming, confusing backslashes in patterns with a ton of slashes.
From the documentation:
Instead of the / which surrounds the pattern and replacement string, you
can use any other single-byte character, but not an alphanumeric character,
\, " or |. This is useful if you want to include a / in the search
pattern or replacement string.
%s:<dog/>:<cat>
You can replace the / delimiters if they become annoying for certain patterns.
Quote them with a backslash. Also, it often helps to use another delimiter besides slash.
:%s#<dog/>#<cat\\>#
or if you have to use slash as the substitute command delimiter
:%s/<dog\/>/<cat\\>/
I was looking for something similar, to search for register values containing the / character (to record a macro). The solution was to search using the ? token instead of the /.
The syntax is:
:%s/<dog\/>/<cat\\>/g
backslash slash backslash star
/(<- the prompt)\/\*
so after you type it looks like
/\/\*

vim search and append/modify

Is there a way to search for a text in vim and then modify, not just overwrite said text? i.e. if each occurrence matches certain pattern, I would like to replace each occurrence with unique text, partly based on characters next to occurrence the text that matches the pattern.
Specifically speaking, I'd like to do the following:
I'd like to replace text that matches the pattern /\*.png with /\*.png alt=* title=\* where * is the text that varies in each case.
Thanks in advance!
You can replace the results with parts of the match by using submatches. For instance, say you have the following html:
<img src="foo_one.png" />
<img src="bar_two.png" />
You can run this command:
:%s/"\(.\{-}\)\.png"/"\1.png" title="\1" alt="\1"/g
The .\{-} will match "any number of characters before the .png part, non-greedily" (:help /\{), and the brackets around it, \( and \) mark it as a captured group that will then be used in the substitution as \1 (the first captured group). Running this gives you:
<img src="foo_one.png" title="foo_one" alt="foo_one" />
<img src="bar_two.png" title="bar_two" alt="bar_two" />
Now, if you need a more complicated substitution, like turning the filename into a human-readable string, you could use the \= substitution flag to replace the string with any expression, like a function call. For instance, here's what we could run on the above text:
:%s/title="\zs\(.\{-}\)\ze"/\=lib#CamelCase(submatch(1))/g
This uses two Vim-specific patterns: \zs and \ze. Those are "match start" and "match end". The thing that will be searched for is the entire pattern, title=.... However, what will be replaced is only between \zs and \ze. This makes it easier to use the result of lib#CamelCase as a direct replacement.
Note that you don't need the \(/\) brackets this time around, because you can just use submatch(0) for "the entire match":
%s/title="\zs.\{-}\ze"/\=lib#CamelCase(submatch(0))/g
The function lib#CamelCase is my own function that's implemented like so:
function! lib#CamelCase(word)
return substitute(a:word, '_\(.\)', '\U\1', 'g')
endfunction
You can write any function that takes some input and returns a replacement string, doing whatever you need.
I'm not sure what your level of experience is with both Vim and regular expressions, so it's hard to give specific advice, and Vim substitutions are a big topic. I can recommend you read up on these help topics:
:help pattern-overview
:help sub-replace-special
:help sub-replace-expression
You can use the command line for that.
In Linux use sed.
sed -i 's/\.png/\.png alt=* title=*/g'
Here it is running a search on .pngand replace by .png alt=* title=*.
You must use the backslash before the . because it's a special character.

How to search and replace using regular expressions in Visual Studio

I need to replace all the urls with empty string:
""regular"": ""http://fonts.gstatic.com/s/abhayalibre/v3/zTLc5Jxv6yvb1nHyqBasVy3USBnSvpkopQaUR-2r7iU.ttf"",
""500"": ""http://fonts.gstatic.com/s/abhayalibre/v3/wBjdF6T34NCo7wQYXgzrc5MQuUSAwdHsY8ov_6tk1oA.ttf"",
""600"": ""http://fonts.gstatic.com/s/abhayalibre/v3/wBjdF6T34NCo7wQYXgzrc2v8CylhIUtwUiYO7Z2wXbE.ttf"",
""700"": ""http://fonts.gstatic.com/s/abhayalibre/v3/wBjdF6T34NCo7wQYXgzrc0D2ttfZwueP-QU272T9-k4.ttf"",
""800"": ""http://fonts.gstatic.com/s/abhayalibre/v3/wBjdF6T34NCo7wQYXgzrc_qsay_1ZmRGmC8pVRdIfAg.ttf""
I've tried using the Regular Expressions with:
"http://fonts(*).ttf"
but i can't see the replace working.
Your mistake is (*), use instead:
http://fonts.+\.ttf
Regular Expression Search and Replace is actually quite well documented.
At the moment you're matching strings that look like this, unless Visual Studio actually fails to parse the expression because of the incorrect usage of *.
http://font).ttf
http://font().ttf
http://font(().ttf
http://font(((().ttf
http://font((((((((((((((((((((((((((((((().ttf
etc
To match any character you could use .*, . being the universal match in Regex, but that will match beyond the closing quotes.
Instead, you can use [^"]+ to match one or more characters except ".
http://font\.[^"]+
Also, note the \. to make sure the regex actually matches the . character, the \ escapes it from being the universal match character.

Vim Search/replace: what do I need to escape?

I'm trying to search and replace $data['user'] for $data['sessionUser'].
However, no matter what search string I use, I always get a "pattern not found" as the result of it.
So, what would be the correct search string? Do I need to escape any of these characters?
:%s/$data['user']/$data['sessionUser']/g
:%s/\$data\[\'user\'\]/$data['sessionUser']/g
I did not test this, but I guess it should work.
Here's a list of all special search characters you need to escape in Vim: `^$.*[~)+/
There's nothing wrong with with the answers given, but you can do this:
:%s/$data\['\zsuser\ze']/sessionUser/g
\zs and \ze can be used to delimit the part of the match that is affected by the replacement.
You don't need to escape the $ since it's the at the start of the pattern and can't match an EOL here. And you don't need to escape the ] since it doesn't have a matching starting [. However there's certainly no harm in escaping these characters if you can't remember all the rules. See :help pattern.txt for the full details, but don't try to digest it all in one go!
If you want to get fancy, you can do:
:%s/$data\['\zsuser\ze']/session\u&/g
& refers to the entire matched text (delimited by \zs and \ze if present), so it becomes 'user' in this case. The \u when used in a replacement string makes the next character upper-case. I hope this helps.
Search and replace in vim is almost identical to sed, so use the same escapes as you would with that:
:%s/\$data\['user'\]/$data['session']/g
Note that you only really need to escape special characters in the search part (the part between the first set of //s). The only character you need to escape in the replace part is the escape character \ itself (which you're not using here).
The [ char has a meaning in regex. It stands for character ranges. The $ char has a meaning too. It stands for end-line anchor. So you have to escape a lot of things. I suggest you to try a little plugin like this or this one and use a visual search.

searching whole word in Vim (dash character)

I know for searching a whole word I should use /\<mypattern\>. But this is not true for dash (+U002d) character and /\<-\> always fails. I also try /\<\%d45\> and it fails too. anyone know the reason?
Edit2: As #bobbogo mentioned dash is not in 'iskeyword' so I add :set isk+=- and /\<-\> works!
Edit1: I think in Vim /\<word\> only is valid for alphanumeric characters and we shouldn't use it for punctuation characters (see Edit2). I should change my question and ask how we can search punctuation character as a whole world for example I want my search found the question mark in "a ? b" and patterns like "??" and "abc?" shouldn't be valid.
\< matches the zero-width boundary between a non-word character and a word character. What is a word character? It's specified by the isk option (:help isk).
Since - is not in your isk option, then - can never start a word, thus \<- will never match.
I don't know what you want, but /\>-\< will match the dash in hello-word.
Could always search for the regex \byourwordhere\b
As OP said. In order to include dash - into search just execute:
:set isk+=-
Thats all.
Example: When you press * over letter c of color-primary it will search for entire variable name not just for color.

Resources