Questions tagged [regular-expression]
Regular expressions (often shortened to "regex") are expressions written in a declarative language used for matching patterns within strings.
574
questions
65
votes
3
answers
25k
views
How to make regex matchers non-greedy?
I am trying to use a regex for replacing text in a file (replace a full url with just protocol/domain/):
:%s/\(https\?:\/\/.*?\/\).*/\1/gc
Unfortunately .*? does not match the string, even trying to ...
59
votes
3
answers
198k
views
Find and replace using regular expressions
I have a file with a bunch of user defaults in. I want to change some of the text, but I'm struggling coming up with a matcher and replacer. Using the following example:
##############################...
35
votes
1
answer
14k
views
Can I search for a Unicode combining character in Vim?
I have a file with the character ã (lowercase a + combining tilde). Encoding and fileencoding are both utf-8. ga shows
<a> 97, hex 61, octal 141 <~> 771, Hex 0303, Octal 1403
(but ...
33
votes
2
answers
5k
views
Is lookaround possible in vim's regex system?
I have encountered many situations in vim where it would be useful to have lookaround statements. I have tried using the standard lookaround syntax ((?=), (?!) etc ..), but it appears vim does not ...
27
votes
0
answers
1k
views
How does `\zs` interact with syntax matches?
Consider the text:
foobarbaz
First we highlight the regular expression /foobar/:
:syntax match Special /foobar/
Next we highlight the regular expression /foobarbaz/:
:syntax match Error /foobarbaz/
...
26
votes
2
answers
7k
views
Regex in Vi: How does one use character classes inside groups? (e.g. [\s\w])
In Vim, the character class \s matches any whitespace character. When I'm using those special characters in groups e.g. [\s\w] it does not match any single whitespace or word character, it does match ...
22
votes
1
answer
15k
views
Deleting a range of n lines before and after a matched line?
I have a repetitive configuration file and I'd like to match a regex and delete a range of lines before, and after a match. I'd also like delete the match in one command.
I can use...
:g/match/-1d
...
21
votes
1
answer
4k
views
Are Vim's regex magics compatible with well-known regex classes?
Many Unix tools' regular expression syntaxes are often the POSIX-codified Basic and Extended Regular Expressions (BRE and ERE, respectively), and, in some modern implementations, Perl-style (PCRE ...
19
votes
5
answers
7k
views
Vim syntax pattern to highlight python keyword argument
I'm struggling with learning vim regexps (I don't know any other regexp very well either), so the main problem is to define regexp that would match
some_function_call(simple_value, keyword=value)
...
19
votes
1
answer
3k
views
Why doesn't Vim regex allow more than 9 capture groups?
From :h E65 we can see that Vim doesn't allow more than 9 capture groups in a substitution command.
For example the following command will work:
s/\v(a)(b)(c)(d)(e)(f)(g)(h)(i)/\9\8\7\6\5\4\3\2\1
...
18
votes
4
answers
3k
views
What is the rationale for \r and \n meaning different things in s command?
We all know that, when searching, \n is newline and \r is carriage return (^M), but when replacing \r is newline while \n is a null byte (^@).
What is the origin of this asymmetry? Given that this ...
18
votes
2
answers
879
views
Is there a way to convert a Vim regex literal to different magic modes?
If I have a magic Vim regex in a string literal, is there a way to convert the whole regex into an alternative representation that uses a different magic mode, so I could paste that equivalent regex ...
17
votes
11
answers
18k
views
Substitute second occurence on line
I have a list of files:
./a.temp.txt ./a.temp.txt
./a/b.temp.txt ./a/b.temp.txt
./a/b/c.temp.txt ./a/b/c.temp.txt
And I want to remove the temp. on each line, but only the second occurence, ...
17
votes
3
answers
2k
views
Ignore URLs and email addresses in spell file
Is there a way to have vim not flag HTTP URLs and email addresses as spelling errors? Or, more generally, a way to list regexes of valid spellings?
17
votes
1
answer
2k
views
Folding by regex search pattern
I got a plain text file with whitespace separated columns of values.
Like this:
AU 3030 .... ... ....
AU 3031 .... ... ....
AU 3032 .... ... ....
AU 3033 .... ... ....
IT 48100 ... .. .....
IT ...
15
votes
3
answers
4k
views
What's the difference between the '\zs' and '\@<=' atoms in Vim regex?
This is what I get from the documentation: \zs "starts the highlighted part" after matching the preceding regex, and \@<= "starts the highlighted part" after matching the ...
15
votes
2
answers
51k
views
Regex to match any character including newline
I frequently find myself trying to do transformations like
author = {{foo
bar}},
to
author = {foo
bar},
and I can't find a regex to match the part in ...
15
votes
2
answers
531
views
How can I run :substitute on only part of the line and not all of it?
I have a line like this one:
function my_function(argument, another argument, but there are spaces)
I want to substitute the space characters (inside the argument's names) with underscore. I thus ...
14
votes
1
answer
2k
views
Apply normal mode command to regex matches
I am using the following regular expression to match a documentation string underneath a Clojure function definition:
\vdefn.*\n\s*\zs"([^"]|\n)*"
Is there a way to run the normal mode gq (format ...
14
votes
2
answers
6k
views
How to search literally without any regex pattern?
Does vim allow for searching raw strings?
If what I want to search is in the variable string, does the following code work?
call search('\V' . escape(string, '\'))
Or are there other more direct ...
13
votes
7
answers
11k
views
How to replace a pair of brackets
I have a file, containing some lines like this: aaa bbb (ccc) ddd.
Now I want to replace the pair of brackets by a pair of square brackets to make this line become aaa bbb [ccc] ddd.
I think that \...
13
votes
1
answer
3k
views
Sort based on comma separated words
Normal command :sort can sort lines based on column or virtual column (\%c or \%v), could the higher level logical column be used as sorting key? Using regular expression looks a little complex for ...
12
votes
1
answer
37k
views
Search for lines starting with given string in Vim?
I have this code snippet:
# import ipdb; ipdb.set_trace()
def foo():
...
import ipdb; ipdb.set_trace()
...
# import ipdb; ipdb.set_trace()
How should I specify search query, that it ...
12
votes
1
answer
4k
views
Usage of the =~ operator
I'm a little confused on VimScript's regex comparison operators.
If I look through :h =~? my understanding is that =~ can be used to do a regex match (with the ? making it case-insensitive).
But ...
11
votes
6
answers
3k
views
Delete n lines skip 1 line script
I want to create a basic Vim script that simply deletes n lines, skips 1 line and repeats until the end of the document. I don't care about specific matches, I just want to delete the line. For ...
11
votes
5
answers
5k
views
How to replace content between two patterns from the file?
I've the following format of the file:
<common>
fitnes=0
genetic=1
method=0
</common>
<inputs>
foo=bar
bar=foo
</inputs>
<limits>
balance=200.00
</limits>
and I ...
11
votes
1
answer
2k
views
What is '\%' and '\@' token's name in vim regex? And what does it do?
While editing syntax file, I found something like:
\s*\%(\%(:\@<!\/\/.*\)\=\|\%(\/\*.*\*\/\s*\)*\)$
I'm familiar with any other than \% and \@ there. I can't even googling because I don't know ...
10
votes
3
answers
389
views
Alternative to escape slashes in search and replace for long strings
Is there any alternative in vim search and replace for strings, than having to escape every special character in string.
For example, If I want to match following string and replace with something
[...
10
votes
2
answers
4k
views
How to write condition with regular expression?
I'm aware of the following comparison:
if @% == "/tmp/crontab.zi5NeVPGRc"
set nobackup
set nowritebackup
set noundofile
set noswapfile
endif
But how can I use regular expression? For example ...
10
votes
2
answers
340
views
How can I make vim stop splitting my words into chunks?
English language users probably don't have this trouble, but vim is pretty dumb when it comes to regular expressions. In my case most letters in the Turkish alphabet outside the EN alphabet don't seem ...
10
votes
1
answer
3k
views
Using a variable in a regex pattern [duplicate]
I'm trying to write a function that will delete each line that matches a pattern. But that pattern will change based on the words held in a list (made from a dictionary). I was thinking it would be a ...
10
votes
1
answer
1k
views
What is an atom?
I have been looking through many resources but cannot seem to find the definition of an atom. In the website link http://vimregex.com/, how does an atom defer from a character in regex?
\{-}
matches ...
10
votes
2
answers
7k
views
Nested matching group in regex
I have a common usecase when I transform some python expression the following way:
value 1
value 2
value 3
into
['value 1', 'value 2', 'value 3']
The easiest way may be to use a mapping, but I ...
10
votes
1
answer
1k
views
Regex that prefers shorter matches within a match? (This is more involved than non-greedy)
I'm trying to create a script for performing fuzzy search inside a buffer. The main idea is to take some input, and insert .\{-} between every pair of characters, e.g. foo becomes f.\{-}o.\{-}o.
This ...
9
votes
2
answers
906
views
Duplicate lines beginning with "x", replacing "x" in the duplicate with "y"?
I have a file with the following pattern:
foo 111
baz
foo 222
baz
foo 333
baz
which needs to be turned into
foo 111
bar 111
baz
foo 222
bar 222
baz
foo 333
bar 333
baz
So, duplicate all the lines ...
9
votes
1
answer
34k
views
How to search for any repeating character, X number of times
Let's say a buffer has a certain characters I know will be repeated seven (7) times. How can I search for any character repeated seven times?
I know I can search for .., but those to characters won'...
9
votes
2
answers
7k
views
Enable incremental search and highlight while typing a search term?
While I enter a regular expression on the vim command line, I would like to see what matches the partial expression I already wrote.
For example, while writing the command
:%s/regular
all words ...
9
votes
2
answers
855
views
How to match a word ending with uppercase letter and a specific letter?
I have words like lblSERINOd in sentences. Words are separated by white space.
I want to replace the d at the end of all such words with u. So for example lblSERINOd will look like lblSERINOu.
I ...
9
votes
2
answers
3k
views
How do I search for a line that contains a pattern at column n
In writing some Vimscript to try to answer this question, I was exploring ways to "search for a line with pattern P in position X" (P is a search pattern, X is a number).
One way to do this would be ...
9
votes
2
answers
325
views
Mixed case regular expression replacement, in Vim
I often run into a situation where I have a file with a lot of mixed case. For example,
CamelCase and camelCase
I'm looking for a Vim regular expression replacement to produce,
AnotherCase and ...
8
votes
4
answers
3k
views
Add constant integer to visually selected numbers
I have a file like the following : two different files were yanked one after another, and the numbers subsequently don't match anymore.
... 46
atom 142 option 47
atom 143 ...
8
votes
4
answers
10k
views
How to delete a multi-line match?
For testing purposes I've opened the following file:
vim http://example.com/
And I'm using the following command to delete a highlighted multi-line pattern:
:/<body\_.\{-}body>/d
which ...
8
votes
4
answers
2k
views
Store the number of matches in VimScript function?
Is there any way to store the number of matches in a variable in a VIMScript function?
For instance I'm using:
%s/,//gn
8
votes
2
answers
591
views
Define syntax in only one line
I would like the first line of some documents I have to be highlighted as a comment.
I would like something like:
syntax match myTypeComment /{apply only to first line}^.*$/
But I don't know how ...
8
votes
2
answers
14k
views
How to regex match beginning and end of a file?
Using /, I want to be able to search for (and jump to) the beginning and end of the file I am in.
8
votes
1
answer
2k
views
How do I replace a character with its hex value?
I want to replace a character with its hex literal. For example before regex:
h
After regex:
0x68
I'm trying this:
s/\([a-z]\)/\=printf("%#02x", submatch(0))/
But this replaces it with
00
...
7
votes
1
answer
2k
views
XOR two lines to see differences
I need to compute a "XOR"-like of two lines to find every char that differs between the two lines. I know the vim-diff feature can easily achieve that, but for comparing two lines I wanted to keep ...
7
votes
3
answers
13k
views
regular expression to find lines containing multiple specific words or patterns in any arbitrary order
Suppose we have the following very simple file:
1 x 3 x
x 3 x 1
Now I'd like to have a pattern that matches all lines containing both 1 and 3. The order and position should not matter.
(Note that I ...
7
votes
2
answers
203
views
Executing a command on delimited lines
How can I run a command on all lines delimited, say by
\begin{otherlanguage} and \end{otherlanguage}?
For example,
1 This line should not be affected,
2 \begin{otherlanguage}
3 but ...
7
votes
1
answer
6k
views
Escape quotes within quotes in command
I'm trying to run a command (from within a function) that goes somewhat like this:
execute a:firstline . ',' . a:last line . 'g/\(\.\|!\|?\)\()\|]\|"\|'\)*\($\|\s\)/execute "normal! v(:call ...