Questions tagged [regular-expression]
Regular expressions (often shortened to "regex") are expressions written in a declarative language used for matching patterns within strings.
55
questions
20
votes
1
answer
3k
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)
...
18
votes
4
answers
2k
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 ...
63
votes
3
answers
23k
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 ...
17
votes
3
answers
1k
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?
16
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 ...
13
votes
8
answers
10k
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 \...
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
34
votes
1
answer
12k
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 ...
26
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/
...
23
votes
2
answers
6k
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
2
answers
509
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
2
answers
5k
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
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 ...
8
votes
2
answers
5k
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 ...
7
votes
1
answer
391
views
Putting the value of an argument into part of a regex in vimscript?
Is it possible to pass a variable to a function in vimscript, and then use that value in a search and replace regex?
For example:
function! AddTW5Links(tiddlerSetName)
%s/\(*\+\)\(\[\[\(.*\)\)\...
4
votes
1
answer
307
views
Traditional Vi and its regular expressions
I'd just want to get confirmation on (and maybe some background to) the way that Bill Joy's vi, sometimes found as traditional-vi on some BSD systems, handles regular expressions.
It seems as if most ...
31
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 ...
20
votes
1
answer
14k
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
...
14
votes
11
answers
16k
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, ...
12
votes
2
answers
42k
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 ...
10
votes
2
answers
326
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
1
answer
29k
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'...
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 ...
7
votes
4
answers
9k
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
5
answers
6k
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
3
answers
877
views
Run a function once on every paragraph
I'm trying to write a plugin that highlights overused words.
After much effort I managed to write a function (and a command, :Ditto) that will highlight the most used word in the file or in a ...
5
votes
2
answers
907
views
What exactly does \%(...\) and \z(...\) do?
I know \(...\) is Vim's way to create a capturing group. It was listed under the atom entry in Vim's doc. There are two other forms of atom, \%(...\) and \z(...\). The document is not very clear on ...
5
votes
1
answer
2k
views
How do I parse URL's from the text
I have a multiline text file. Some lines have URL's.
What would be the most efficient way to extract those URL's?
I wrote macros and did a bunch of unnecessary things.
Now I'm thinking about regex ...
5
votes
1
answer
541
views
Is it possible to turn on spell-checking for only parts of buffers?
It seems that spell-checking in vim is normally enabled at a buffer level (set spell vs. set nospell). However, sometimes it would be useful to ignore certain parts of a buffer (for example, URLs in ...
4
votes
2
answers
2k
views
Delete all of a file except for certain words that contain certain letters
I have a file that contains words that I want to save, along with other junk that I do not need. I just want to delete everything except the words that contain a certain pattern. Take email addresses ...
4
votes
1
answer
514
views
Vim ErrorFormat question for Gradle compiler plugin
I am trying to fix a compiler plugin for Gradle. Gradle generates error messages of the type:
:compileJava/file/path/Main.java:52: error: cannot find symbol
public static ABC abc;
...
4
votes
4
answers
10k
views
delete repeated line in vim, keep the n-th line in Vim
I want to ask how to delete repeated lines in vim.
For now, I have a file which contained 12740 lines. There are only 98 unique lines, and each line is duplicated 129 times.
I have tried:
g/^\(.*\)$\...
4
votes
2
answers
312
views
Is it possible to close all files with content that matches a regular expression on Vim?
Let's say I have opened many files on Vim with vim file1 file2 file3... And their content is like the following:
file1:
random content
random content
stringThatMatchesRegex
random content
file2:
...
4
votes
2
answers
274
views
How do I remove lines where its content already occured in the previous line?
For example I've the following text:
This is my first line, but longer.
This is my first line
This is my second line, but longer.
This is my second line
This is my third line, but longer.
This is my ...
4
votes
4
answers
470
views
How to run a python command based on a matched group of a pattern?
Let say I want to run a command that uses a matched group of a pattern for each line matching the pattern.
The natural choice is :g.
With the command :s, there is no problem. If i would do
:g/aaa\(....
4
votes
1
answer
655
views
Substitute all except the last asterisk in groups of two or more asterisks
How can I substitute all except the last asterisk in groups of two or more asterisks with two spaces? E.g.
* Micro topic 1
** Microbes are small
** You can't see them!
*** Isn't that something?
...
4
votes
1
answer
397
views
Insert subsequent numbers in a substitution pattern
I want to insert subsequent numbers whenever there a regex matches.
I do this in terminal using for command and either perl or sed.
Is there a way to do this in vim?
Basically I want this:
This ...
3
votes
1
answer
263
views
Why don't these multiline regular expressions using a positive lookbehind work?
EDIT: Don't waste time reading this question unless you're interested in a good example of why you should always check the :help first: a severe case of PEBKAC.
Consider the text:
foo
.
.
barbaz
...
3
votes
2
answers
276
views
Can't repeat negated pattern using \@!
I have this text:
```python
x = 1
```
Basically markdown python cells. I would like to extract the content of the cell surrounding the cursor, if any.
As you may have guessed already, I am trying to ...
2
votes
1
answer
282
views
How can I find where one word is close to another word?
If I want to find occurrences of the word 'arc', I can type / arc , with spaces around the three letters, to find basic occurrences of the word (ignoring punctuation). But suppose I want to find where ...
2
votes
4
answers
2k
views
Search for line, move to end of search string, then search+replace
(EDIT: In the original question, I had accidentally used \w instead of \<. Comments and answers with \w should be interpreted as using \< as well.)
In my LaTeX source files, section headings ...
2
votes
1
answer
952
views
Syntax highlight multiple matches in the same line
I would like to know how to highlight multiple tokens in the same line.
For example, if I want to highlight the value of 'a', I can write something like this:
syn match num 'a=\zs\d\+\ze,b=\d\+.*'
...
1
vote
1
answer
328
views
How do I use :Tabular only for delimiters after a certain pattern?
How can I use the Tabular plugin to align by ";" after "//"?
With text:
Line 1 ; B // Comment 1 ; Comment 2
Line 2 C // Comment 3 ; Comment 4
After running :Tabularize /; I get:
Line 1 ...
1
vote
1
answer
635
views
How do I use regular expressions in ternary statements / keymappings?
I'd like to create a keymapping with a conditional statement, so that it behaves differently when a given line is blank.
This thread gave me the following snippet, which is great – except that it ...
1
vote
4
answers
814
views
In Vim ex mode how to re-use a string from the default buffer in the command
I'm trying to modify this file, by extracting the very first line in the file and prepend it to each of the consecutive lines until end of the file
black
widow
card
Friday
berry
I used ex command (...
1
vote
1
answer
238
views
Duplicating a row and modifying the duplicate as a macro or regex
Given a file with multiple lines matching this pattern (apologies for the length!)
$this->execSQL('INSERT INTO `table_lowercase_and_underscores` (`id`, `field1`, `field2`, `field3`, `field4`, `...
1
vote
1
answer
81
views
Yank both sides of equation
While writing in latex I often want to copy a math equation's right hand side or left hand side. For example I have something like this
\begin{gather*}
5 \cdot \left( A A 3 \right) + 50 \cdot \...
1
vote
2
answers
454
views
How to create regex group with dependency with earlier group via math operations
Imagine I have a file as follows,
10 10
10 11
...
10 20
20 20
20 21
...
20 40
40 40
40 41
...
I know simple regex such as
To search two numbers
/\d\+ \d\+
To search repeated numbers
/\(\d\+\) \1
My ...
1
vote
2
answers
594
views
Regex missing matching words
I am trying to add some custom highlighting to my Vim. But my regex matching is not working correctly.
let ignoreHiInComments = '^\s*\(\/\/.*\)\@<!' "Ignore highlighting in C++ comments
call ...