Reputation: 2350
I'm trying to get a regular expression to work in Vim but I can't seem to get it right. I need to run this on a file that has too many lines for Sublime or Atom to handle.
This is just a portion of it to find the first five digits of each line.
^(\d{5})
In Vim, I tried running the following but get no matches. I've tried a few variations but can't get it right. What am I doing wrong?
:%s /(\d\{5})/
Upvotes: 1
Views: 152
Reputation: 1553
I think very magic is what you are looking for.
:%s /\v/(d(5))/<replacement>/<flags>
Hope this helps
Upvotes: 0
Reputation: 58
:%s/\v^(\d{5})/The first 5 digits are \1/
Consider :help magic
because if you come from regexing in different languages, different things need to be escaped.
Also, remember you can test your regex live by using the /
or ?
matching with hlsearch on set hlsearch
. The highlight search helps you identify exactly where your regex breaks down since it highlights live, as you build the regex.
Once you have the regex working, hit enter to apply it, enter command mode for your %s
swap and use CTRL+R
to select a registry, and then /
to put in the search you just tested.
Upvotes: 0
Reputation: 213
Search and replace - vim.wikia
When searching:
+, ?, |, &, {, (, and ) must be escaped to use their special function.
So technically your command should be
:%s /\(\d\{5\}\)/
Upvotes: 0
Reputation: 627082
You just need to escape the (
and )
. Unlike other regex flavors, Vim regex uses (
and )
as literal parentheses and when you escape them, they are treated as a grouping construct.
4.5 Grouping and Backreferences:
You can group parts of the pattern expression enclosing them with
"\("
and"\)"
and refer to them inside the replacement pattern by their special number\1, \2 ... \9
.
Upvotes: 3