Questions tagged [regular-expression]

Regular expressions (often shortened to "regex") are expressions written in a declarative language used for matching patterns within strings.

558 questions
64
votes
3 answers

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 escape the ? quantifier? How should non-greedy…
guido
  • 1,757
  • 1
  • 20
  • 25
31
votes
2 answers

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 support this. Does vim have an equivalent syntax…
EvergreenTree
  • 8,180
  • 2
  • 37
  • 59
24
votes
2 answers

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 s or w. How does one use character classes inside…
JHK
  • 755
  • 7
  • 12
21
votes
1 answer

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 ...several times to delete more than one line from…
leeand00
  • 3,555
  • 5
  • 24
  • 40
20
votes
1 answer

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 being an implementation of this). Is there a…
muru
  • 24,838
  • 8
  • 82
  • 143
19
votes
1 answer

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 But this one with one more capture group will…
statox
  • 49,782
  • 19
  • 148
  • 225
18
votes
2 answers

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 into source code? For instance, if I want to…
Mu Mind
  • 485
  • 3
  • 10
14
votes
2 answers

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 ways?
doraemon
  • 1,667
  • 11
  • 27
14
votes
2 answers

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 between the curly…
oarfish
  • 1,277
  • 3
  • 10
  • 18
11
votes
1 answer

What is '\%' and '\@' token's name in vim regex? And what does it do?

While editing syntax file, I found something like: \s*\%(\%(:\@
Mas Bagol
  • 527
  • 1
  • 4
  • 14
9
votes
2 answers

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 anotherCase Right now I do this in two steps after…
wsams
  • 193
  • 5
9
votes
2 answers

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 have tried s/.*\ud /u /g but Vim says it cannot find…
Utku
  • 305
  • 2
  • 6
7
votes
1 answer

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 everything in the same…
nobe4
  • 16,033
  • 4
  • 48
  • 81
7
votes
3 answers

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 used a very simple file on purpose. The file could…
myrdd
  • 203
  • 1
  • 2
  • 7
6
votes
2 answers

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.
WalksB
  • 507
  • 1
  • 4
  • 14
1
2 3
8 9