How can make gvim identify "/" in the search pattern [duplicate] - regex

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

Related

Notepad++ regex replace - how to remove this string

I want to remove strings in the form of the following where some-text is a random text string.
$('#some-text').val();
I've tried various things but I think the $ sign is messing things up since it's used in regex.
You need to escape some characters.
Try this -
\$\('#[^']*'\)\.val\(\);
Try this regex by escaping special chars:
\$\(.*\).val\(\);
To avoid escaping the special characters, you can use \Q - \E pair to surround the part where you want the regex engine to interpret literally:
\Q$('\E<your-regex>\Q').val();\E
Replace <your-regex> with your regex to match the selector, or whatever it is.

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 ;)

How can I search a text using regex excluding a character?

I want to search a text using regex using \S but excluding the "," at the end.
p.e.:
text, tàta réta
My regex must find text (without the ",") tàta réta
I tried this but it didn't work:
/\<\S+[^,]
\w doesn't work also, because it doesn't find the non ascii characters èéòà etc.
Read up on
:he /zero-width
I think with default options you meant (note the \+ instead of just +)
/\<\S\+[^,]
This won't work because + is greedy by default, and the comma is not a whitespace :)
I suppose this would do what you want:
/\<\S\{-}\>,\#!
With verymagic, this could be a whole lot cleaner
/\v<\S+>,#!

Slashes and hashes in Perl and metacharacters

Thanks for the previous assistance everyone!. I have a query regarding RegExp in Perl
My issue is..
I know, when matching you can write m// or // or ## (must include m or s if you use this). What is causing me the confusion is a book example on escaping characters I have. I believe most people escape lots of characters, as a sure fire way of the program working without missing a metacharacter something ie: \# when looking to match # say in an email address.
Here's my issue and I know what this script does:
$date= "15/12/99"
$date=~ s#(\d+)/(\d+)/(\d+)#$1/$2/$3#; << why are no forward slashes escaped??
print($date);
Yet the later example I have, shows it rewritten, as (which i also understand and they're escaped)
$date =~ s/()(\d+)\/(\d+)\/(d+)/$2\/$1\/$3; <<<<which is escaping the forward slashes.
I know the slashes or hashes are programmer preference and their use. What I don't understand is why the second example, escapes the slashes, yet the first doesn't - I have tried and they work both ways. No escaping slashes with hashes? What's even MORE confusing is, looking at yet another book example I also have earlier to this one, using hashes again, they too escape the # symbol.
if ($address =~ m#\##) { print("That's an email address"); } or something similar
So what do you escape from what you don't using hashes or slashes? I know you have to escape metacharacters to match them but I'm confused.
When you build a regexp, you define a character as a delimiter for your regexp i.e. doing // or ##.
If you need to use that character inside your regexp, you will need to escape it so that the regexp engine does not see it as the end of the regexp.
If you build your regexp between forward slashes /, you will need to escape the forward slashes contained in your regexp, hence the escaping in your second example.
Of course, the same rule apply with any character you use as a regexp delimiter, not just forward slashes.
The forward slashes are not meta characters in themselves - only the use of them in the second example as expression separators makes them "special".
The format of a substitute expression is:
s<expression separator char><expression to look for><expression separator char><expression to replace with><expression separator char>
In the first example, using a hash as the first character after the =~ s, makes that character the expression separator, so forward slash is not special and does not require any escaping.
in the second example, the expression separator is indeed the forward slash, so it must be escaped within the expressions themselves.
The regex match-operator allows to define a custom non-whitespace-character as seperator.
In your first example the '#' is used as seperator. So in this regex you don't need to escape the '/' because it hase no special meaning. In the second regex, the seperator char isn't changed. So the default '/' is used. Now you have to escape all '/' in your pattern. Otherwise the parser is confused. :)
If you are not use slashes, the recommend practice is to use the curly braces and the /x modifier.
$date=~ s{ (\d+) \/ (\d+) \/ (\d+) }{$1/$2/$3}x;
Escaping the non-alphanumerics is also a standard even if they are not meta-characters. See perldoc -f quotemeta.
There is another depth to this question about escaping forward slashes with the s operator.
With my example the capturing becomes the problem.
$image_name =~ s/((http:\/\/.+\/)\/)/$2/g;
For this to work the typo with the addition of a second forward slash, had to be captured.
Also, trying to work with just the two slashes did not work. The first slash has to be led by more than one character.
Changing "http://world.com/Photos//space_shots/out_of_this_world.jpg"
To: "http://world.com/Photos/space_shots/out_of_this_world.jpg"

How does one escape backslashes and forward slashes in VIM find/search?

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