Questions tagged [regular-expression]

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

Filter by
Sorted by
Tagged with
22
votes
3answers
59k 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: ##############################...
21
votes
2answers
2k 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 ...
17
votes
4answers
4k 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) ...
16
votes
1answer
2k 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 ...
15
votes
2answers
3k 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 ...
15
votes
2answers
437 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 ...
15
votes
1answer
3k 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 ...
15
votes
0answers
318 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 /...
14
votes
1answer
1k 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 ...
14
votes
1answer
512 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 ...
13
votes
4answers
1k 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 ...
12
votes
3answers
5k 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 ...
11
votes
3answers
816 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 preceding atom. But I don'...
11
votes
1answer
5k 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 ...
11
votes
1answer
1k 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 ...
11
votes
1answer
2k 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 ...
10
votes
3answers
275 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
1answer
650 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?
9
votes
2answers
632 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
5answers
2k 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 ...
9
votes
2answers
531 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
1answer
1k 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 ...
9
votes
2answers
245 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 ...
9
votes
1answer
2k 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 ...
9
votes
1answer
742 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 ...
9
votes
1answer
438 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 ...
8
votes
10answers
4k 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, ...
8
votes
1answer
982 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 ...
8
votes
2answers
221 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 ...
8
votes
2answers
4k 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 ...
7
votes
2answers
177 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
1answer
1k 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
1answer
21k 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 ...
7
votes
1answer
1k 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 ...
6
votes
3answers
213 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 ...
6
votes
2answers
2k 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 ...
6
votes
2answers
2k 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 ...
6
votes
3answers
793 views

Delete block of code if a pattern is (not) detected in one of the lines

I have managed to do this with a super long find-replace but I'm sure there's a simpler way: I have a file of the form: E01 2017 10 24 00 00 00-1.90001e-04-8.79280e-12 0.00000e+00 3....
6
votes
3answers
5k 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 ...
6
votes
4answers
565 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
6
votes
1answer
527 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 ...
6
votes
2answers
166 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 ...
6
votes
1answer
612 views

Vim to highlight only a submatch

Folks, I'm writing a syntax highlighting script for vim, but I'm struggling on how to highlight only a submatch of a pattern. For example, suppose that I want to highlight any number within #, like #...
6
votes
1answer
92 views

Convert word to another substituting case (upper or lower) with same case

I am looking for a way to convert one word to another while "capturing" the case of the match that is substituted. An example: I have the text: Begin the beginning Now I want to convert this to ...
5
votes
5answers
3k views

Match any number of digits NOT followed by

I want to search for: From start of the line, any number of white spaces followed by any number of digits NOT followed by a dot. And replace it with all that was searched and a dot. This is just ...
5
votes
4answers
2k views

Run global substitute for each match of a regex, with increment

I've got a somewhat obfuscated javascript file with multiple variables named a##########, where # stands for a single 0-9 digit. Each such identifier may or may not be unique. I was substituting ...
5
votes
4answers
6k views

How to delete part of a line that does not match a pattern?

I'd like to remove everything apart of my pattern match. For example I've the following line: window.page4_callback(["here \"is\" some text"]); and I'd like to leave the text only within the double ...
5
votes
2answers
6k views

Regex to match spaces that are NOT at the beginning of a line?

I want to remove repeated spaces, but not if they occur at the start of a line (indendation). Before: line 1 hi there i have spaces After: line 1 hi there i have spaces I've ...
5
votes
2answers
569 views

Vim conceal indentation. Replace indentation spaces with utf-8 chars

I use four spaces for indent. I would like to replace the indentation with some UTF-8 chars. What I have: 1. def foo(bar): for jaffa in bar: print jaffa 2. var foo = function(bar){ ...
5
votes
1answer
485 views

How do I ignore regex failure while running a macro?

If I have a regex in a recorded macro and then run it over a line that the regex fails to match the macro stops. For example, I have a list of files. family-reunion-2015.webm road-festival-20150607....