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
0answers
38 views

Regex expression to replace date (YYYY-MM-DD) to month name, day, 4 digit year [closed]

I have a Google Docs file that contains diary entries starting with a date formatted as YYYY-MM-DD. To import into a dedicated diary application, I need to change the dates to the long name of the ...
2
votes
1answer
22 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
2answers
415 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
1answer
21 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
1answer
46 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
0answers
13 views

Storing Latex Chunks in Registers

Previously I asked about how one could yank both sides of an equality into registers. My use case was writing latex and wanting to be able to take work that I previously just wrote and modify it ...
0
votes
0answers
26 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
1answer
54 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
1answer
41 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 ...
1
vote
1answer
77 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.
2
votes
1answer
44 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
1answer
28 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
1answer
59 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 ().
0
votes
2answers
31 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
1answer
74 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
2answers
67 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 ...
0
votes
1answer
99 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
1answer
44 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
1answer
17 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
0answers
22 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
0answers
28 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
1answer
45 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
1answer
24 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 ...
2
votes
1answer
60 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
1answer
33 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
1answer
23 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
1answer
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?
1
vote
1answer
86 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 ...
0
votes
1answer
42 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,...
1
vote
1answer
57 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?
6
votes
1answer
112 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
27 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
111 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
43 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 ...
3
votes
2answers
137 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
320 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
31 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
35 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
45 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
23 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
128 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
116 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
124 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
43 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
148 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
18 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
101 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
166 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
87 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
48 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^#+.*(#.*)&...

1
2 3 4 5
10