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
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) ...
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 ...
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 ...
15
votes
2answers
436 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
0answers
315 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 /...
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?
6
votes
1answer
535 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
5
votes
1answer
212 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/\(*\+\)\(\[\[\(.*\)\)\...
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 ...
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 ...
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 ...
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 ...
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 ...
5
votes
1answer
842 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 ...
4
votes
3answers
516 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 ...
3
votes
1answer
166 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
4answers
3k 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/^\(.*\)$\...
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 ...
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 ...
4
votes
2answers
199 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
1answer
429 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
1answer
324 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
1answer
2k 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 ...
3
votes
1answer
353 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; ...
3
votes
3answers
1k 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 ...
2
votes
1answer
611 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
1answer
159 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
1answer
27 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
2answers
311 views

Highlight exported Go functions

Golang uses naming convention to separate functions visibility: exported (public) starts with a capital letter, non-exported (private) with a lower letter. I would like to highlight exported functions,...
0
votes
1answer
147 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 ...