Skip to main content

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
3 answers
192 views

Search and replace multiple characters simultaneously

Suppose I have a buffer containing a b c How can I use a single search and replace command to obtain the following (cyclic) permutation? b c a In this question, I found one can use the "bar&...
907456's user avatar
  • 13
0 votes
2 answers
37 views

search('map <F\d>.','W') fails to find pattern in my .vimrc

Mostly I invoke my key-mappings with a function key followed by a single character. Finding an unused combination gets tedious, so I'm writing a mapping to list all such combinations in my .vimrc But ...
user985675's user avatar
1 vote
2 answers
54 views

Substitute a word in which cursor in the current line

How can I :substitute, in the current line, a word on which cursor is, but only after the cursor position. e.g.: when doing (illustratively, may not exact correct command): :s/<C-r><C-w> ...
user17227456's user avatar
0 votes
1 answer
24 views

Neovim commands or plugins for an equvialent of the :s command with different regex syntaxes

I would want a different regex syntax, like JS regex for instance, with a command for or like :s/ commands.
Oneechan69's user avatar
0 votes
1 answer
25 views

Search and replace within first occured curly braces

I have a text like this in lines {4 5 6} {8 9 7} {1 2 3 34 67} {45 85 66 44} I would like to replace the lines with negative numbers inside the first curly braces {-4 -5 -6} {8 9 7} {-1 -2 -3 -34 -67}...
flash 's user avatar
  • 25
1 vote
4 answers
276 views

Double numbers on all matching lines containing text pattern

Is there an easy way to search for all lines matching a pattern, extracting a number on the line, and modifying it? I am playing around with a game config file and want to double all of the XP numbers ...
NewUser's user avatar
  • 93
1 vote
2 answers
73 views

How do I make Vim do spell-check only in words that beings with [a-zA-Z] and continues on with [a-z]?

Suppose I am writing in a markdown file, and I am using odd naming conventions like 'thisFileNameIsGood'. Spell checker will also mark it as wrong along with the names that the spell checker doesn't ...
souraj ghosh's user avatar
3 votes
1 answer
137 views

Regex searching with escaping

Given the following 3 lines "unit": "mm" "unit": "s" "unit": "%" If I do a regex search /"unit": "[^sm]", it would ...
Chung's user avatar
  • 133
0 votes
1 answer
23 views

Usage of pattern for ilist command

I am trying to work with a large JSON file and I would like to print the lines with an occurrence of a any one of two particular strings. For this I'm using ilist, for one string I can do as follows ...
Daniel's user avatar
  • 101
2 votes
2 answers
136 views

Do we need to escape backslash in character in Vimscript?

As title, specifically, is it correct that one of the backslashes in the following regex is redundant in vimscript: syntax match MyGroup "[a-zA-Z0-9_:\\]" I tried to ask Copilot but while ...
NeoZoom.lua's user avatar
  • 1,530
2 votes
3 answers
125 views

Remove blank lines at end of file

How can I augment/modify my trailing whitespace logic to also remove any empty or all whitespace lines at the end of a file? My current whitespace stripping logic in my .vimrc is: "remove ...
Dave's user avatar
  • 123
1 vote
2 answers
55 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
123 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
128 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
157 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
532 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
85 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
  • 227
1 vote
1 answer
52 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
  • 207
0 votes
1 answer
37 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
89 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
74 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
57 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
492 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
66 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
47 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,244
3 votes
3 answers
2k 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
  • 289
1 vote
1 answer
199 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
134 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
111 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
  • 466
0 votes
1 answer
123 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
33 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
  • 650
0 votes
2 answers
102 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
177 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
95 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,150
1 vote
1 answer
661 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
142 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
55 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,150
2 votes
2 answers
985 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,150
1 vote
1 answer
224 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,150
0 votes
1 answer
489 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
104 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
  • 513
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
38 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
248 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
5 votes
1 answer
445 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,383
0 votes
1 answer
43 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,244
0 votes
1 answer
852 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
478 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
2 3 4 5
12