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
1
vote
1answer
29 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?
5
votes
0answers
49 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
1answer
24 views

Can't replace div using regex

I have the following code from lines 6-20 <div> 1 </div> <div> 2 </div> <div> ...
1
vote
0answers
36 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 ...
2
votes
1answer
34 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 ...
2
votes
2answers
92 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
2answers
273 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
2answers
29 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 ...
0
votes
0answers
25 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
0answers
39 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
1answer
21 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 ...
2
votes
2answers
72 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
2answers
85 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 ...
0
votes
1answer
27 views

Language specific custom syntax highlighting

So I have to follow a name convention for my C data types : Typedefs must start with a t_ prefix such a t_mytype. Structs must start with a with an s_ prefix and so on for other data types. So I added ...
1
vote
1answer
28 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("...
3
votes
1answer
139 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 ...
-1
votes
1answer
10 views

Automate joining lines in text blocks (perhaps with SHIFT+j) but do not join blocks [duplicate]

I have the lastest versions of arch and nvim which I am using. I have a text that looks like this the slow brown fox jumped over a bridge the blind brown fox stumbled upon a badger ...
0
votes
2answers
46 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 ...
0
votes
2answers
41 views

How do I ignore specific words in a search and replace regex?

I am using this working regex :%s/\( to\)\([a-z]\)/\1 \2/c to replace tothem with to them and tohim with to him. But I do not want to look at words like together, tone, tonal, etc I tried something ...
0
votes
0answers
31 views

syntax highlight regex not matching

I am writing a plugin where a certain filetype must possess their own syntax highlight. Therefore I have created . ├── ... ├── plugin └── syntax and syntax/newfiletype.vim containing syntax clear ...
1
vote
1answer
25 views

User <cword> inside a vimgrep regex expression

Writing a map to find header under cursor for markdown link. ## heading {#foo} ... ... [click on link](#foo) <- `cursur above this foo` What I am doing: nnoremap gO :lvimgrep "\v^#+.*(#.*)&...
3
votes
1answer
181 views

Join multiple lines based on pattern

I have text bounded by quotes as follows "This is an example of some text" I want to join these into one line. e.g. "This is an example of some text" I have tried commands like ...
1
vote
1answer
26 views

Can you call highlight more than once? Need different colors for different matches

I am working with text files and Goyo. I'd like to highlight two different kinds of notes delimited by brackets (one called "NOTE:" and the other "TODO:") in different colors. I ...
0
votes
0answers
45 views

Spelling mistake regex

I have a large text file of an OCR'd book where many words have been broken and hyphenated at the end of a line. Eg. In his newest book of language ex- ploration, Professor Asimov ranges over I can ...
2
votes
0answers
18 views

Vim replace repeating pattern [duplicate]

I got this repeating pattern, but when trying to replace it in vim like: %s/<pat>\_.*<\/pat>// It will replace from first to the very last . How to replace these pattern by pattern? My ...
1
vote
1answer
12 views

\s doesn't work in matchstr

I have this in my init.vim: autocmd TextChangedI <buffer> echom matchstr(getline(line('.')), "^\s*y") I was expecting to get the first part of the line if I enter something like that: ...
0
votes
2answers
32 views

Test string after a pattern

I want to test, if the cursor is currently in a comment line. For example in C it looks like this: // This is a comment line now it's also possible that it's a big comment section: /* * Big comment ...
2
votes
1answer
41 views

Convert working commands to VIMRC friendly mappings (ie: :%s with regex)

Is there a simple way of taking a previously issued command and converting it into a .vimrc-friendly keybinding? For example, I have a simple substitution utilizing regular expressions that won't work ...
1
vote
1answer
19 views

Why does this pattern make :global work only on the first line of the highligted match?

\/\*.*\(\n\s\*.*\)*\n\s\*\/ is a pattern which I cooked up to match some multi-line C comments, i.e., in the format of: /* Comment Text * Comment Text * Comment Text */ Searching for the pattern ...
1
vote
2answers
236 views

Replace regex group in nvim

I'm using the most recent version of nvim on arch. I have sentences in my text like this The brown fox I wish to use this format :%s///g to replace all lines starting \nlowercase letter + rest of ...
1
vote
1answer
860 views

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.
2
votes
2answers
318 views

Regular expression for searching IP addresses

I opened up a document in vim and I want to search the document for IP addresses. So I used this command /[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3} But this pattern doesn't seem to return any of ...
0
votes
2answers
24 views

append things to the end of a pattern

I have several instances of variables in a file and I need to modify the variable and append "new" to each of the names. var Modelfrfr = .... var Modeldede = ... var Modelenen = ... ...
2
votes
1answer
94 views

why doesn't regex work in vimscript (while it does in normal search)?

I have simple regex to search for lines that only contain alphabetic characters: ^\(\a\)\{1,}$ which works in normal search /^\(\a\)\{1,}$ but not when invoked from my script: "in .vimrc file: ...
1
vote
1answer
23 views

How to replace [ with ( in vim

I have this markdown line, ![Home][screenshots/home.png] I want to replace it with, ![Home](screenshots/home.png) So, I tried this, :%s/[s/(s/g Vim(version 81) says pattern not found.
1
vote
1answer
35 views

How to recursively open all files except ones in hidden directories from within vim?

In my python project root folder, there are multiple directories, some of which contain hidden sub-directories specifying python virtual environments. I want to open all my python files recursively ...
2
votes
0answers
55 views

Can Ultisnips regular expression match forward?

The question may be also phrased as, can Ultisnips regular expression match after the current cursor? My goal is to have a postfix of curly bracket for LaTeX subscripts. Say I want to type a_{bcd}, ...
-1
votes
1answer
84 views

Regex for any character that is not alphanumeric, whitespace, or a doublequote

I expect this to select all non-alphanumeric characters: [^\w] Instead it selects all non-w's. This is the regex I would use in JavaScript, Python etc.: [^\w\s"] How can I write this so that it'...
0
votes
2answers
152 views

Change date format from MM-DD-YYYY to YY-MM-DD using a regular expression?

A file contains 03-01-2020 04-01-2021 03-02-2020 04-02-2021 03-03-2020 04-03-2021 03-04-2020 04-04-2021 03-05-2020 04-05-2021 03-06-2020 04-06-2021 03-07-2020 04-07-2021 03-08-2020 04-08-2021 03-09-...
1
vote
0answers
41 views

Multi-line regex with specific beginning and end?

I can't seem to find the answer to this anywhere. I'm trying to get started with include-search, and since I'm working in golang right now, I'm trying to get vim to include a set of imported files. E....
1
vote
1answer
125 views

UltiSnips regex macthing

I want the possibility of transforming an inline equation in latex to a display equation. So I want $something$ to become $$ something $$ My attempt was this: snippet "$(.+)$" "...
0
votes
1answer
63 views

regex and matchstr to get final directory and file in path

I am wondering about a regex that would enable me to get the final "directory/file" from a path. I'll explain the reason why. After using vim for 20+ years, I've finally had cause to edit &...
0
votes
3answers
58 views

Is it possible to exchange search patterns in vim?

Is there a way, built-in or with a plugin, that I can swap any number of two different patterns on an arbitrary range using a regex? Maybe something :s-style like :{range}exchange/{pattern-1}/{pattern-...
1
vote
2answers
172 views

Trying to undestand why using \zs in regex is not working as I expect

Consider the following test String: County,Age,Age_group,Gender,Jurisdiction,Travel_related,Origin,EDvisit,Hospitalized,Died,Case_,Contact,Case1,EventDate,ChartDate,ObjectId If use the following ...
0
votes
2answers
81 views

How do you :tag: every word of a line using substitution?

How do you substitute every word of a line, so that it becomes a useful vimwiki tag? Provided the following line: Word Compund_Word I want: :Word: :Compund_Word: I guess it involves some use of ...
1
vote
1answer
32 views

Is there a function which returns the filepath at the cursor?

I'm looking for a function(probably doesn't exist) which can return the filepath under the cursor. I know that there is a command/shortcut gf that helps me jump to that file if the file exists. But I ...
1
vote
2answers
155 views

Remove a pair of brackets without using plugins [duplicate]

I am looking for a way to remove a pair of brackets in the insertion mode without plugins. It's like (|) When I press the delete key only the first bracket get deleted |) But I want to auto-remove ...
1
vote
3answers
173 views

Convert sed regex to vim regex

I got this sed regex I found: sed '/start/!d;s//&\n/;s/.*\n//;:a;/end/bb;$!{n;ba};:b;s//\n&/;P;D' which basically outputs the content that is between a starting point and an end point. ##1## ...
1
vote
1answer
91 views

Pipe :Ag output to a new buffer

I'd like to search for a regex using Ag. This results in more than 1000 findings. I'd like to save these findings in a buffer to be able to go through them slowly
1
vote
2answers
87 views

Vim equivalent to Perl regex for making a substitution in a substitution?

I have this perl regex: s/(^(#+)\s.*?\g2)/$1 =~ s|\n| |rg/gmse It strips out all new lines between ##+ characters and replaces them with a space. It'll take a string like: before text more text ### ...

1
2 3 4 5
9