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
0 votes
1 answer
2k views

How to delete all lines that start with a `\`?

Here is (part of) a file that I am working on turn down\ turn off\ turn on\ \pard\pardeftab720\ri-6\sl-660\qc\partightenfactor0 \f0\b\fs32 \cf0 U\ \pard\pardeftab720\ri-6\sl-240\qj\partightenfactor0 \...
3 votes
1 answer
1k views

Match one regex pattern if not preceded by another one

I'm trying to match \item \macro{this\var}.% YYYY-MM-01 where 01 can be 1 and replace with \item The Month is MM \item \macro{this\var}.% YYYY-MM-01 when NOT this case: \item MonthYY \item \macro{...
2 votes
2 answers
37 views

How do I get substitute() function to work on individual lines like s///g

When I select a range visually and run this command: :'<,'>s/\(.*\)\s\+#.*$/\1/g It removes the text at the end of each line that starts with # which is what I want. I want to run a vim function ...
13 votes
8 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 \...
3 votes
1 answer
164 views

Capitalizing a backreference in a search (NOT a substitute)

I am looking for a way to match any line in a csv file that contains an identifier (e.g. OfficeLocation) and then, later on, the same identifier in all caps. A line to be matched might look like: ...
3 votes
2 answers
515 views

How do I replace single whitespaces without affecting multi-whitespace such as indentation in Vim?

I have textfiles with paths that have lost underscores between words inline in an OCR process. I want to keep the indentation and put back the underscores in the paths. I have a path like this: /...
0 votes
1 answer
140 views

Replace interpolated variables in Python string with positional formatting strings

How do I, with one regex, transform: logger.info(f"Foo {alpha}") logger.info(f"Foo {alpha} bar {beta}") logger.info(f"Foo {alpha} bar {beta} baz {gamma}") Into: logger....
3 votes
1 answer
980 views

Delete all lines not matching a multi-line selection

I'm aware that :g/<body/ .,/body>/d can be used to delete everything within the multi-line selection betweeh <body and body>. I'm also aware that :g!/<pattern>/d can be used to ...
0 votes
0 answers
164 views

Regex multiple lines search, matched, but highlighted only what initially is on screen

Let's say I have the following fantasy code // a long definition of a structure struct long_struct { ... }; There are tons of multiple lines inside such block of code. It cannot be displayed ...
0 votes
1 answer
674 views

How do I sort lines after a prefix?

I would like to sort all lines in a file based on where the text of the line starts, after a line index which is printed into the file, like this: 126. This is the start of the line. It was ...
0 votes
1 answer
46 views

How can I connect separated lines without highlighting in visual mode?

I would like to connect broken lines, like: This is a line that continues. but instead of highlighting them with visual mode and connecting with Shift-J, I would like to run some kind of global ...
2 votes
1 answer
998 views

How do I replace all newline sections of any length with a single newline?

I would like to replace every section of newlines - for example, a single newline at the end of a line, or 5 newlines at the end of a line until the next line of text - with a single newline. I tried ...
1 vote
1 answer
189 views

How do I enter N carriage returns during a substitution?

When using :s, how do I multiply the string being written by an integer, to write N copies of it? For example, :s/pattern/10*\r/ would write 10 carriage returns in each match.
1 vote
1 answer
84 views

Why is asterisk not highlighting repeated occurences of a character?

I am using the latest version of Gvim on Windows 10. When I type / and some expression, for example /a, I see all matches in the document highlighted. The documentation says that the asterisk symbol * ...
4 votes
1 answer
538 views

Character class (square brackets) ignored while matching regex

Why []([a].*) matches [](b)? I would expect it to require a to be the first character in ().
7 votes
1 answer
1k views

Matching any Unicode whitespace

I'd like to match in Vim any whitespace character, not only normal space (U+0020) and tab (U+0009) with \s, and the other ASCII ones like form feed (U+000C) with [[:space:]], but any Unicode ...
0 votes
2 answers
211 views

Illegal back reference when trying to delete \1

I have a bunch of expressions (matlab anonymous functions) of the form, for example `f\^I = @(x,y,z) x^y + z`, `g1\^I\^I = @(z,b,c) z^b + c` (where \^I means a tab, but I can't just write that in ...
2 votes
1 answer
122 views

Remove duplicate spaces except those belonging to initial indentation?

Summary: How to remove duplicated spaces in visual selection and/or globally while preserving the indentation? Imagine you have a mess like this: line of text indented line of text and ...
2 votes
2 answers
170 views

Divide a line with curly braces that contains curly braces

The input line contains pairs of curly brace and in each pair of curly braces there might also be curly braces in the contents. Multiple layers of curly braces may also happen. Here's some example ...
3 votes
1 answer
3k views

How to match paragraphs of text ending with a character?

This problem that seems trivial is beating me real hard. I'm trying to create a couple of syntax highlighting directives for a custom language that is structured in paragraphs separated by at least ...
0 votes
3 answers
112 views

VIM Macro to delete all Perl POD?

POD is a markup format that is used by Perl. It's sometimes found embedded within the code. You can find more information on it from the official docs on perlpod, and perlpodspec. I'm wondering if ...
0 votes
1 answer
1k views

Regex for all spaces that not surrounded by quotes

I'm trying to write a regex that matches all spaces except those enclosed in quotes. I found solutions on the Internet for various programming languages, for example, here, but I am having trouble ...
2 votes
1 answer
117 views

What is the difference between character classes \d and [:digit:] in a regular expression?

I studied a tutorial for vim syntax files and found this example: " Floating point like number with E and no decimal point (+,-) syn match celNumber '[-+]\=\d[[:digit:]]*[eE][\-+]\=\d\+' I was ...
1 vote
1 answer
63 views

Why did \a match some non alphabetic characters

As my understanding, in the regex of Vim, \a matches any of alphabetic character. Now I'm trying to change the line file_exts = [tex, log, aux,] into file_exts = [(tex), (log), (aux),] I've tried to ...
0 votes
0 answers
161 views

Matching patterns in autocommands

I'm trying to match a particular path pattern for an autocommand. I have this: au BufRead *.md if expand('<amatch>') =~ "/path/to/dir/[0-9]" It successfully matches, eg. c:\soemthing-...
0 votes
0 answers
29 views

Insert newline (\n) at search and replace but get "^@" instead [duplicate]

I want to set a linebreak after all braces }. I tried to replace the braces by newline \n with: :%s/\}/\n/ But all I got is a ^@ instead of a new line. Example: abc{def} foo bar{baz} and so on I ...
0 votes
2 answers
339 views

Given a set of patterns, list (in the quickfix window) the ocurrences in those files that contain all the patterns from a given directory

The context Given a {pattern}, I can list the ocurrences of {pattern} in all files from a {directory}, in the quickfix window with the following command :chdir {directory} :vimgrep /{pattern}/ **/* :...
0 votes
1 answer
441 views

Using "(?=<)" in regex leads to "E866: (NFA regexp) Misplaced ?"

The following command should output the string "st": :echo matchstr('test', '\(\?<=e\).*') However, it leads to the error mentioned in the title. Reading :h E866 didn't help much. I also ...
1 vote
1 answer
31 views

replace simple pattern with sequential next line from another file

My file working.txt has 1000's of lines, which repeat in a simple pattern as follows: name date_ax name date_cd name date_ed name date_dc ...etc I need to replace each instance of name with the ...
1 vote
1 answer
100 views

Insert and remove a marker at the start of the current line

So, I'm trying to set up a sort of bookmark/marker kind of workflow, and I came to notice I'm used to put characters such as # and such for different meaning inside my own notes, etc. Now, I already ...
2 votes
1 answer
66 views

Moving by word (w/b) and staying within the line

Background I'm attempting to remap the arrow keys to move by word while staying within the current line. I.e. I'm looking for a behavior similar to w and b, but that acts like h and l in the sense ...
0 votes
1 answer
157 views

How to create an abbreviation that matches a regular expression

I'd like to create an abbreviation that will change the following (in a markdown file): - list item 1 into this: - list item 1 Whenever I type - f it should be expanded to - f (from 1 space to 3 ...
0 votes
1 answer
36 views

How to put sync region start=<this> into a variable?

I have the following code: com! -nargs=* ShFoldFunctions <args> fold ShFoldFunctions syn region shFunctionOne start="^\s*[A-Za-z_0-9:,][-a-zA-Z_0-9:]*\s*()\_s*{" end="}&...
1 vote
1 answer
34 views

How do I do substitution against just part of a larger pattern?

I have a string like 18NTC101L , which I can search by \v[0-9]{2}[A-Z]{3}[0-9]{3}L. Now, I just want to replace the L at the end with letter T. How I can do that?
0 votes
1 answer
656 views

Add or remove single quotes around numbers in yaml files with vim [duplicate]

I'm editing some large YAML files with neovim, and need to add (and remove, in some cases) single quotes around some values. For example - code: 1234 needs to be changed to code: '1234' in other cases,...
2 votes
2 answers
451 views

Is there a way to use matchadd() to highlight multiple lines without highlighting newlines?

Suppose I have this in a text buffer (vim --clean mydoc.txt): One Two Three Four I highlight all lines using :call matchadd('ErrorMsg', '.*'), resulting in: Notice that the newline character on each ...
1 vote
1 answer
884 views

Why does /^[\s]* not work how I expect?

I use vim as my $MANPAGER. I will often use :/^[ ]*-F (for example) to quickly jump to a flag and read up on it. However, the equivalent :/^[\s]*-F doesn't return the same results. Why not?
1 vote
0 answers
262 views

Bulk file rename in Vim Visual Block Mode

I am using VIM Renamer plugin to bulk rename files, I want to replace E008_r5_Haz_Area_Class NAT GAS.pdf E009_r4_Haz_Area_Class.pdf E010_r4_Haz_Area_Class MP STEAM.pdf E011_r3_Haz_Area_Class.pdf ...
0 votes
1 answer
116 views

Can't replace div using regex

I have the following code from lines 6-20 <div> 1 </div> <div> 2 </div> <div> ...
2 votes
1 answer
273 views

Caret `^` and `\%V` compatibility in Vim regex

Can the caret ^ and the sequence \%V be used to match the beginning of a line in a visual selection? And if not, what is the rationale? The following example seems to indicate that ^ and \%V are not ...
4 votes
2 answers
334 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: ...
1 vote
2 answers
1k views

How to match all C code comments in a file?

I want to match (search) all C code comments in a file in e.g. enum { foo, /* Foo is so cool, why can't we all be more like foo? */ bar, /* but bar is mysterious and ...
0 votes
2 answers
105 views

How to replace a sequence of multiple words in multiple lines with a condition of another word in the same line?

I need your kind help on writing a regular expression for the following situation. Consider the following first word sequence: Criteria A xxx xxx xxx Table1 Criteria A xxx xxx xxx Table2 Criteria A ...
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/ ...
1 vote
0 answers
117 views

Jump between all the matchadd matches for a group?

One can use the matchadd function to add to groups of matches that will be highlighted in the VIM editor. But how does one jump between the matches within a specific group?
0 votes
0 answers
56 views

How to add spaces upto a fixed column before a certain character in the line

Suppose I have a line in which the dot appears. Then, I want to add spaces upto fixed number of columns to indent my code. Also, I can't install plug in. Is there any way I can do with just command ...
0 votes
1 answer
161 views

Regex macro to remove only lines NOT containing specific content

Given a word list, I would like to delete all lines that do not have a specific content. For illustration, In the following list, I would like to delete all lines that do not end in s. bird chairs ...
1 vote
2 answers
231 views

Copy to clipboard or save to file substitution command

I'm trying to answer a vim question in U&L, which involves a very complex and long regex substitution. I would like to copy to clipboard the command, or save it in a file, so I don't have to make ...
1 vote
1 answer
121 views

How to match brackets in matchadd command regular expression?

I want to highlight breakpoint() - including brackets as keywords in Python files. I have added these two commands to my .vimrc: highlight link PyBreakpoint Keyword call matchadd("...
0 votes
2 answers
688 views

Append a line after matched multiline pattern

i want to run this global command : /3)\(.*\n\?.*\)\{1,10\};;/ which matches multlines. I want to append a line after the matched block(of lines) across multiple buffers. I don't know where to begin ...

1 2
3
4 5
12