Questions tagged [regular-expression]
Regular expressions (often shortened to "regex") are expressions written in a declarative language used for matching patterns within strings.
551
questions
1
vote
1
answer
41
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 ...
1
vote
4
answers
71
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 ...
1
vote
1
answer
37
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?
...
1
vote
1
answer
66
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
...
0
votes
1
answer
12
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&...
1
vote
1
answer
36
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
...
1
vote
0
answers
56
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 ...
2
votes
1
answer
47
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 ...
3
votes
1
answer
424
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 ...
0
votes
2
answers
43
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>
...
1
vote
0
answers
34
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 ...
3
votes
3
answers
157
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-...
1
vote
1
answer
53
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('~') .. '*/...
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 ...
2
votes
2
answers
132
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\(....
0
votes
2
answers
69
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 ...
0
votes
1
answer
45
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(
...
0
votes
1
answer
28
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 ...
0
votes
2
answers
62
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 ...
0
votes
1
answer
47
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 ...
1
vote
1
answer
81
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 ...
1
vote
1
answer
132
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 ...
0
votes
1
answer
40
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 ...
0
votes
3
answers
45
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,}\)...
2
votes
2
answers
693
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 ...
1
vote
1
answer
102
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 ...
0
votes
1
answer
54
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 ...
0
votes
1
answer
51
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 ...
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=...
0
votes
1
answer
32
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 ...
1
vote
1
answer
137
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\ '!....
3
votes
0
answers
128
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"...
0
votes
1
answer
25
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:
...
0
votes
1
answer
387
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 ...
0
votes
2
answers
251
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 ...
1
vote
1
answer
35
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, ...
0
votes
2
answers
69
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 ...
2
votes
3
answers
340
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 ...
0
votes
2
answers
202
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',
...
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 ...
2
votes
1
answer
144
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?
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
1
vote
1
answer
502
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 ...
2
votes
1
answer
60
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
...
2
votes
1
answer
372
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
1
vote
1
answer
32
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 ...
2
votes
1
answer
2k
views
Delete all text before a specific character using vim
I have used curl to grab a website's code and put it in a text file. I'd like to delete all text that isn't a link! Lucky for me, all links are inside quotes, so I just need to delete all lines ...
0
votes
3
answers
100
views
How can I select all non-lowercase multiline blocks which follow a keyword?
I'd like to copy these to a file, but in a speculative future case,
I might want to delete them. I might need to find a regex to substitute
intervening data with a single blank line.
The highlighting ...
4
votes
1
answer
242
views
Upcase only the 3 and 4 letter words
I've written a script to convert 3 and 4 characters words, since they often are abreviations in the texts I work on.
Here is the starting text :
Diabetes - Hbp - Copd -
I want to transform it to :
...
1
vote
1
answer
27
views
matching with single character > or lines that start with >\s
I want to delete any lines that either match ^>$ or >\s
I have something like this, but this doesn't work:nnoremap dq :.,$g/^>[\s$]/d<CR>
Here's a small sample where it should delete ...