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
2 answers
31 views

Insert newline after each line with a search pattern depending on the line before it

Basically, how can I add log statements like this with Vim? Initial document (pseudocode): case VALUE_1: doFunc1(); case VALUE_2: doFunc2(); doAnotherFunc(); case ...
xaxa's user avatar
  • 177
1 vote
4 answers
111 views

Vim mapping to make function parameters become multi-line?

I'm looking for a minimal way to convert code that looks like this: my_function(param1, param2=value2, param4=a_function_call()) into something like: my_function( param1, param2=value2, ...
beyarkay's user avatar
  • 205
1 vote
2 answers
90 views

With regex, how to match misused blankspace inside tab indent

Say the line is: ^<space1><space2><tab1><tab2><space3><space4><tab3><tab4><space5><space6><tab5><tab6><space7><space8&...
Qiu Yangfan's user avatar
1 vote
2 answers
145 views

Search for all hotkeys starting with "g" in Vim's "quickref.txt"

Within the quickref.txt (which can be reached by typing in :tab help quickref) I can see plainly texts, pages after pages, that look like: h N h left (also: CTRL-H, <BS>, or &...
eliu's user avatar
  • 113
2 votes
2 answers
512 views

How to validate and reference digraph /special character in Vim?

I want to remove the ending section of my output that typically follows the below pattern: ^[[2D[^[D but my regex will not allow me to select the ^[ parts of the word. Note on my Vim that part shows ...
alejandro_hagan's user avatar
1 vote
4 answers
80 views

How to encapsulate a visual selection with preprocessor logic?

I have to preprocessor-comment out a lot of C-code. E.g. I want this: if (abcd == true) do_smt(); To become this: #if 0 if (abcd == true) do_smt(); #endif This is tedious work, so I was hoping ...
Opifex's user avatar
  • 217
1 vote
1 answer
45 views

Is there a way to substitute a text that has regex formatted number ranges and replace with all combinations?

Let's say I have a file that looks like: host[1-2]lx host[1-4]win what I want is an easy way to get host1lx host2lx host1win host2win host3win host4win Is there a neat way to automate that process? ...
deedar huq's user avatar
1 vote
1 answer
71 views

How can I replace duplicate line prefixes with blanks?

Is it possible to transform: John Apple Red 3 John Apple Green 5 John Radish White 2 John Radish Red 7 Tom Apple Red 3 Tom Apple Green 5 Tom Radish White 2 Tom Radish Red 7 to: John Apple Red 3 ...
athos's user avatar
  • 135
0 votes
1 answer
22 views

What is the syntax to use character classes inside Vim regex bracket expression?

I did the following tests: code output notes :echo substitute("s s", '\v[^s]', "<&>", "g") s< >s space replaced. All fine :echo substitute("s s&...
KamilCuk's user avatar
  • 265
1 vote
1 answer
57 views

How can I match a regexp containing newlines?

When developing a vim plugin I'll publish soon, I want to highlight a text contained in a variable g:last_completion_text. Here below the code to define an highlight group named PrompterCompletion ...
Giorgio Robino's user avatar
1 vote
0 answers
68 views

\zs not working in syntax file?

Udated 2023-09-11: Changed example to asterisk as it is simpler and the question now refers to \zs instead of \ze as \zs seems to be the issue. I am trying to adapt the VIM Markdown syntax file so ...
StaticNoiseLog's user avatar
2 votes
1 answer
53 views

difficult regex pattern (for me) to suppress

I have a giant svg file with "two" colors. I need to suppress one of the colors, but it is a purge to do it graphicaly with Inkscape for example. So I try to do it by suppress the path ...
sh_k_'s user avatar
  • 23
3 votes
1 answer
464 views

"/\v[\w]+" cannot match every word in Vim

Target text: "This is a sample text with some words like hello123 and bye456." I want to match each word in that text. /\v[\w]+ in Vim is bad, it can only match every "w". What is ...
littlecode's user avatar
0 votes
2 answers
52 views

How to convert a string into a list of matching and non-matching substrings?

I have a list that I'm trying to filter. I'm planning to separate a string into parts of the string that matches a pattern and parts that doesn't. Like if the string is: <h1>hello</h1> ...
Samuel Bacay's user avatar
1 vote
0 answers
39 views

Interaction between zero-width matchers and backreferences

Take a file with the following text as a workbench. hello world hello world With the cursor at the beginning of the line, here's how \1 interacts with \(…\), \@=, and \@! # regex matched text text ...
Enlico's user avatar
  • 2,194
3 votes
3 answers
1k views

Use regex to substitute a match except the first and last characters

Maybe the title isn't to explicative; feel free to improve it. I have the text below: 1. Intro |timetracking-intro|$ 2. Uso |timetracking-usage|$ 3. Comandi |timetracking-...
Antonio's user avatar
  • 277
1 vote
1 answer
135 views

Negate pattern in autocmd

I have some lua auto command that triggers whenever I open a file that has */some_pattern/* in the path. vim.api.nvim_create_autocmd({ 'BufEnter' }, { pattern = vim.fn.expand('~') .. '*/...
Stein's user avatar
  • 143
0 votes
1 answer
36 views

Searching a non repetitive in Vi [closed]

I'm quite new to Linux as well as to vi so I have several questions. Assume the text is as below: Hi one and one. Hi one. Is there anyway to search the first line where the word one exist twice in ...
Aaron Tan's user avatar
2 votes
2 answers
133 views

Just the first line is deleted

Given the following file: Stand a b c d e f Stand a b c I would like to remove the lines that match the following pattern Stand\(.*\n\)\{4} To achieve that I executed the command :g/Stand\(....
Ayoub Falah's user avatar
0 votes
2 answers
88 views

Tabular regex for '\]\s\a' align

I am trying to align the following lines of code from this: reg aaa; reg [DATA_W-1:0] bbb; reg ccc; to this: reg aaa; reg [DATA_W-1:0] bbb; reg ccc; Using Tabular plugin. I ...
xyx's user avatar
  • 412
0 votes
1 answer
73 views

Using variable to find & replace while incrementing number with regex

Given the following text: 0 : Container( alignment: Alignment.center, padding: const EdgeInsets.all(24.0), child: RichText( text: TextSpan( ...
AdjunctProfessorFalcon's user avatar
0 votes
1 answer
31 views

Pattern works with / but it does not work when used in filter function()

I want to extract the the substring Python 3.11 from the strings like Python 3.11.2.release0 (i.e. I want to extract Python followed by the first two digits separated by a .). Such longer strings ...
Barzi2001's user avatar
  • 498
0 votes
2 answers
85 views

Capturing a group in a pattern used in a Vim autocmd

I need Vim to ignore the .tmpl file extension to enable syntax highlighting. How can I rewrite this: autocmd BufNewFile,BufRead *.lua.tmpl set filetype=lua to be more general? I want to match the lua ...
Nikita M. Grimm's user avatar
0 votes
1 answer
108 views

How do I include the match number when searching and replacing in vim using regex?

I'm trying to make my struct's members accessible through an index like an array, so if I have a struct like this in rust: pub struct SubjectsProgress { pub sword: i32, pub lance: i32, pub ...
Apoqlite's user avatar
  • 103
1 vote
1 answer
87 views

Why very magic flag can't take effect on character = in substitute command?

We can see the explaination on very magic :With very magic, all ASCII characters except 0…9, a…z, A…Z and _ become a a special meaning. In this mode, searching for text becomes very magic. When you ...
showkey's user avatar
  • 1,120
1 vote
1 answer
332 views

What does \_. means in Vim?

what does \_. means in vim ? explain to me character by character Response from chatgpt: In Vim regular expressions, \_. is a sequence that represents any character including a newline. Here is an ...
andrew_ysk's user avatar
0 votes
1 answer
69 views

Apply normal command to all regex matches on the same line

This comment convinced me that I don't need multiple cursors, however, it's missing one feature that multi-cursor can do, which is to perform actions on multiple regex matches on the same line. :g is ...
Wong Jia Hau's user avatar
0 votes
3 answers
50 views

Strange result for grouping replace in a line

The file contains only one line: four_blank_space + "TE:" + one blank space + "trailers" + one blank space I write a grouping replace: :1s/\(\s\+\)\(.\+\):\s\+\(.\{-}\)\(\s\{0,}\)...
showkey's user avatar
  • 1,120
2 votes
2 answers
845 views

Why [\w-] can't match \w or -?

[ab] matches any of a or b ,\w matches any word character (alphabets, digits, underscore). I find that [\w-] can't match \w or - .In order to convert Sec-Control into "Sec-Control".The ...
showkey's user avatar
  • 1,120
1 vote
1 answer
167 views

How to match a specific column position till the end of line?

^ denote the beginning of the line ,$ denote the end of the line. Suppose that there is a line in my vim: this is a test line test line. I want to remove all the characters from the 20 th character ...
showkey's user avatar
  • 1,120
0 votes
1 answer
270 views

Use regex pattern as argument to function

I want to be able to pass a plain regular expression like so autocmd BufReadPost * call TextEnableCodeSnip('bash', /^hello/, /^world/, 'SpecialComment') To a function and use it within as a regular ...
drcomputer's user avatar
0 votes
1 answer
70 views

Simplifying a complex regex

I am programmatically generating a very lengthy regex expression in a lua script I'm developing. It works, but at 960 characters, I can't help thinking it can be simplified a bit. However, I can't ...
Phil R's user avatar
  • 471
0 votes
3 answers
44 views

Replace all occurrences of ' 1 ' with matched text in one line

I've got a text file, containing following lines <a href="tg://proxy?server=radan.rooznameh.etellaate.rooz.romatism.sbs&port=443&secret=...
wl2776's user avatar
  • 360
0 votes
1 answer
34 views

Substitute in multiple isolated parts of a string

I have this string: b'32^"Strings" c32 d32 c32 b32 r8. \break I want to replace [a-z] (except r) with r and remove ' everywhere outside "..". I want to ignore \break. Final output ...
kargirwar's user avatar
  • 145
1 vote
1 answer
187 views

How to use alternation operator in ripgrep + vim?

I have the following in my .vimrc which (I believe) makes :grep within Vim use rg: if executable('rg') set grepprg=rg\ --no-heading\ --vimgrep\ --hidden\ --case-sensitive\ --ignore-vcs\ --glob\ '!....
Aaron Parisi's user avatar
4 votes
1 answer
258 views

How do I turn on `\v` by default for Vim/Neovim regexes?

After years of using Vim and then Neovim, I still find myself forgetting to escape parenthesis and the | character when I intend to use their special behaviour: If I want to search for "this"...
Flimm's user avatar
  • 2,202
0 votes
1 answer
33 views

How can I match 0 or more atoms and never give it back to backtracking?

I've noticed that matchstr('hello*', '\v\a+\*=\ze%(\A|$)', 0) returns hello, which means that \*= is not matching the literal *. I thought I had got the syntax wrong, but I think I've not, because: ...
Enlico's user avatar
  • 2,194
0 votes
1 answer
577 views

Regex for replacing parameter values in a URL (values between = and &)

I have a large list (100,000+) of URLS with parameters like so: https://www.example.com?a=b&1=2&me=you https://www.example.com?a=b I'm using VIM need a regex to replace all values with ...
ex7lted's user avatar
  • 37
0 votes
2 answers
353 views

Regex to select indented lines preceded by one top blank line not working in Vim

I made the regex: /^\n\t(\n|.)*?\n$/gm (prepended with \v inside Vim) to select consecutively indented lines (with tab) separated by a top blank line. But while it works in other languages it doesn't ...
ThisMakesMeGray's user avatar
1 vote
1 answer
43 views

Return last match of matchstr within single line

I have a little class/function detector in my .vimrc displaying, in my statusline, the python class/function my cursor currently is inside of. I am currently trying to adapt it for C++ source files, ...
Feffe's user avatar
  • 1,761
0 votes
2 answers
77 views

How can I chain together regular expression substitutions

I am trying to write a command that changes author lists of the form R. L. Rivest, A. Shamir, and L. Adleman Into one nice for BibTeX, namely: Rivest, R. L. and Shamir, A. and Adleman, L I have ...
sdpoll's user avatar
  • 103
2 votes
3 answers
350 views

Regex: is this a Vim bug or my bug?

I have a syn match item like this: syn match txr_num "[+\-]\?\([0-9]\|[0-9][,0-9]*[0-9]\)\?[.]\([0-9]\|[0-9][,0-9]*[0-9]\)\([eE][+\-]\?[0-9]\+\)\?" Given an input like 1.234e13 it matches ...
Kaz's user avatar
  • 481
0 votes
2 answers
274 views

How would I remove all text outside single quotes in VIM using regex?

I'm using a vim script to quickly edit some text. I have a large list from an api endpoint that looks like this: save: presets.api + 'campaign/:campaignId/settings', ...
ex7lted's user avatar
  • 37
1 vote
2 answers
68 views

How to create a range command that substitutes characters with grouping and back references?

The use case is to copy citations from pdf files to markdown documents in vim. I'd create a dotted list of important copied text and then I'd like to replace the end of line hyphenation that appears ...
Paul Rougieux's user avatar
2 votes
1 answer
162 views

How can I store the number of regex matches in a variable?

In Vim Script, I want to check for the amount of matches from a regular expression (/\%^\n*) and store the amount of matches in a variable. I this possible?
Amarakon's user avatar
  • 261
5 votes
4 answers
4k views

Regex to delete all leading blank lines

Is there a Vim regex to delete all leading blank lines in the file? text more text some more text Should become: text more text some more text
Amarakon's user avatar
  • 261
1 vote
1 answer
774 views

Regex replace for equal sign in whole file

I'm trying to do a simple replace for all equal signs and subsequent characters until end of line: %s/\=.+\n/\n/. However this gives E866: (NFA regexp) Misplaced =. If I try instead to simply replace ...
Sos's user avatar
  • 113
2 votes
1 answer
67 views

Snippet lookahead trigger

Using UltiSnips, I'm wondering if it's possible to do some lookahead assertion when detecting the trigger. For example, this works: snippet "dv" "derivative" ir \dv{$1}{$2}$0 ...
Alex867's user avatar
  • 33
2 votes
1 answer
439 views

Break line after the n-th comma

Suppose I have the next line 1,0,10,10,9,7,7,5,41,3,57,7,1,9 I want to split it into several lines, each containing n commands for example, for n = 4 1,0,10,10, 9,7,7,5, 41,3,57,7, 1,9
juaninf's user avatar
  • 153
1 vote
1 answer
36 views

Replacement for ^M when scripting with Vim

I am creating a script that grabs all links from a web page's source code. I would like to call Vim from a bash script and the easiest way for me to do that is to use a vim script. I have a file ...
ex7lted's user avatar
  • 37

1
2 3 4 5
12