Simple Regex slipped up by Pile Delimeter? - regex

I've a simple regex to create and for the life of me cant get it to work due to the Pipe Delimiter causing headaches.
I'm just trying to add a file extension, by looking at a previous extension, and adding that to the end of the current name.
(Find)
old.jpg|new
(Replace)
old.jpg|new.jpg
I've used a good combination but having the Pipe seems to trip me up.
Are there any suggestions or tips?

You have to escape the pipe, either with \| or [|].

The | character has special meaning. You need to escape it, for example by using a character class: [|].

Related

How to get each function pattern in a file?

I have a file contains a lot of functions, each function like
=======
CREATE PROCEDURE [fName]
code...
code...
END
GO
=======
I want to get every function pattern, and the expression I used is
^(CREATE PROCEDURE).*(\r\n.*)+\r\nGO
The result is wrong. Can somebady help! Thanks for answer
Using sed:
sed -n '/CREATE PROCEDURE/,/^END$/{H};/^GO$/{s/.*//;x;p;}' input
Try something more like:
(?<=^|\n)(CREATE PROCEDURE [\s\S]+?)(?=\r\nGO|$)
This will match every instance of CREATE PROCEDURE that starts a new line and the text that follows it, up to but not including the next GO directive, or the end of the file, whichever comes first.
In [\s\S]+?, the question mark makes this a non-greedy match, which is how we stop at the next GO instead of the last GO. [\s\S] is equivalent to [.\n], i.e., it matches any character.
I've assumed .NET-style regular expressions, since you didn't specify.
instead of using "." and \r\n try using [\s\S] it matches anything that is a whitespace OR isn't a whitespace including newlines.
CREATE PROCEDURE[\s\S]*GO
This may need some regex flags to be set

My Vim replace with a regex is throwing a `E488: Trailing characters`

I'm trying to find all instances of a Twitter handle, and wrap an anchor tag around them.
:%s/\(#[\w]\)/<a href="http://www.twitter.com/\1">\1<\/a>/gc
Which gives me:
E488: Trailing characters
If you have this when replacing within a selected block of text, it may be because you mistakenly typed %s when you should only type s
I had this happen by selecting a block, typing : and at the prompt :'<,'>, typing %s/something/other/ resulting in :'<,'>%s/something/other/ when the proper syntax is :'<,'>s/something/other/ without the percent.
When the separator character (/ in your case) between {pattern} and {string} is contained in one of those, it must be escaped with a \. A trick to avoid that is to use a different separator character, e.g. #:
:%s##\(\w\+\)#\0#gc
PS: If it should do what I think it should do, your pattern is wrong; see my correction.
I had this issue and couldn't make it go away until I found out that the .vimrc file that I had parts that I copied from else where that contained abbreviations, like this for example:
abbrev gc !php artisan generate:controller
That abbreviation would mess up my search and replace commands which usually look like this:
:%s/foo/bar/gc
by expanding that gc into !php artisan generate:controller, except, that it wouldn't do it on the spot/ in real time. The way that I clued in was by looking through the command history (by pressing : and the up arrow) and seeing
:%s/foo/bar/!php artisan generate:controller
So if you're getting trailing character errors no matter what you do I'd look inside
~/.vimrc
and see if you can find the problem there.
I had the same problem.
Only using other delimiters didn't help. So, additionally
I didn't select any row.
And didn't use g for global.
so just
:%s#to_be_replaced#replacement#
did the job. Changed all occurrences of 'to_be_replaced' with 'replacement'.
:%s/\/apps/log_dir/g
where string to replace=/apps
and replaced string=log_dir
as we saw / so we need to use "\/"
This is how I caused my "E488: Trailing characters"
Occasionally the muscle memory in my brain skips a beat as it did today causing me to seek the reason for my E488: Trailing characters.
:%s/searchItem/changeTo/s
The s at the end caused my E488: Trailing characters.
I should have used a g
:%s/searchItem/changeTo/g
Placing the g at the end worked as always.

Regular expression - return string after equal sign and before a pipe

I attempted to resolve this on my own, but I need some help.
I have a strong that is located in my syslogs - and while I know grep would be easier, I have my reasons for building a regular expression.
For example: somerandomstufffilter-category=Web_Advertisements|andhereis more random stuff
In that garbled mess, there is an exact string 'filter-category=Web_Advertisements|'
I created this regex: (filter-category=).*?(?=["|"])
Which pulls the exact string I want, and up to the pipe. Instead of giving me the entire string, I only want everything after the equal sign (and not include it in my match), and before the pipe.
Right now I am getting: 'filter-category=Web_Advertisements'
What I want to get is: 'Web_Advertisements' on its own. The trick is that whatever follows filter-category, can be anything, not just Web-Advertisements. That is why it MUST end with the first pipe it encounters.
You don't need to capture filter-category=, so drop the ().
You don't need to use a lookahead here, so drop the (?=...).
By the way, you were misusing character classes by writing ["|"]—that would have meant you were going to match either a double-quote " or a pipe |.
Here's what you want:
filter-category=(.*?)\|
acheong87's answer could include pipes in the capture... if it's greedy and there is more than one pipe, it will capture everything up to the last pipe.
Try
filter-category=([^\|]*)\|
Like acheong87's answer, this will match the whole of
filter-category=Web_Advertisements|
from which you have to extract the part you want to keep, captured into $1

How to search (using regex) for a regex literal in text?

I just stumbled on a case where I had to remove quotes surrounding a specific regex pattern in a file, and the immediate conclusion I came to was to use vim's search and replace util and just escape each special character in the original and replacement patterns.
This worked (after a little tinkering), but it left me wondering if there is a better way to do these sorts of things.
The original regex (quoted): '/^\//' to be replaced with /^\//
And the search/replace pattern I used:
s/'\/\^\\\/\/'/\/\^\\\/\//g
Thanks!
You can use almost any character as the regex delimiter. This will save you from having to escape forward slashes. You can also use groups to extract the regex and avoid re-typing it. For example, try this:
:s#'\(\\^\\//\)'#\1#
I do not know if this will work for your case, because the example you listed and the regex you gave do not match up. (The regex you listed will match '/^\//', not '\^\//'. Mine will match the latter. Adjust as necessary.)
Could you avoid using regex entirely by using a nice simple string search and replace?
Please check whether this works for you - define the line number before this substitute-expression or place the cursor onto it:
:s:'\(.*\)':\1:
I used vim 7.1 for this. Of course, you can visually mark an area before (onto which this expression shall be executed (use "v" or "V" and move the cursor accordingly)).

How to match a decimal letter and blank in vim?

I need to change
1 A
2 B
3 C
4 D
to
A
B
C
D
which means the decimal letter at the begining of every line and the following one or more blank should be deleted .
I'm only familiar with Reqex in Perl, so I try to use :%s/^\d\s+// to solve my problem, but it does not work. so does anyone of you can tell me how to get the work done using vim ?
thanks.
Vim needs a backslash for +, so try
:%s/^\d\s\+//
One way is to use the global command with the search-and-replace command:
:g/^[0-9] */s//
It searches for the sequence:
start of line ^
a digit [0-9]
a space <space>
zero or more spaces <space>*
and then substitutes it for nothing (s//).
While you can do a similar thing with just the search-and-replace command on its own, it's useful to learn the global command since you can do all sorts of wonderful things with the lines selected (not just search and replace).
Use the following
:%s/^[0-9] *//
You should use instead
:%s/^\d\s\+//
Being a text editor, vim tends to treat more characters literally‒as text‒when matching a pattern than perl. In the default mode, + matches literal +.
Of course, this is configurable. Try
:%s/\v^\d\s+//
and read the help file.
:help magic
You can also use the Visual Block mode (Ctrl+V), then move down and to the right to highlight a block of characters and use 'x' to remove them. Depending on the layout of the line, that may in fact be quicker (and easier to remember).
If you still want to use Perl for this, you can:
:%!perl -pe 's/^\d\s+//'
Vim will write the file to a temporary file, run the given Perl script on it, and reload the file into the edit buffer.
Escape the plus sign:
:%s/^\d\s\+//
If it's in a column like that you could go into the column visual mode by pressing:
esc ctrl+q
then you can highlight what you want to delete