Questions tagged [regular-expression]
Regular expressions (often shortened to "regex") are expressions written in a declarative language used for matching patterns within strings.
550
questions
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
...
1
vote
0
answers
55
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 ...
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
...
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 ...
5
votes
2
answers
11k
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.
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
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 ...
1
vote
1
answer
52
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
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-...
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
59
views
Syntax - Match word based on a previous match
I want to highlight all words that have been matched in another syntax region. Using something similar to regex capture groups, for example given a file:
{highlightme} asda asasd asd asd as ...
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
27
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 ...
11
votes
6
answers
2k
views
Delete n lines skip 1 line script
I want to create a basic Vim script that simply deletes n lines, skips 1 line and repeats until the end of the document. I don't care about specific matches, I just want to delete the line. For ...
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
46
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
131
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 ...
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
101
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
526
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
1
answer
53
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=...
1
vote
3
answers
2k
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 ...
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\ '!....
5
votes
1
answer
261
views
How can I edit a regular expression search string as if it were a file?
Sometimes a regular expression starts to get pretty complex, and it's hard to edit in Vim's commandline.
It seems the obvious way is to edit the search term as though it's a file and be able to ...
3
votes
0
answers
126
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"...
4
votes
1
answer
533
views
How to match the content in a pair of brackets, no matter how many pairs of brackets are contained in the content?
I want to replace
GD[many[[brackets]]], GD[evenMore[[[[brackets]]]]]
with
GD[someStuff], GD[someOtherStuff]
How can I match the correct pair of brackets? These patterns are scattered in a large ...
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
386
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 ...
15
votes
3
answers
3k
views
What's the difference between the '\zs' and '\@<=' atoms in Vim regex?
This is what I get from the documentation: \zs "starts the highlighted part" after matching the preceding regex, and \@<= "starts the highlighted part" after matching the ...
4
votes
1
answer
456
views
Set variable equal to search match subsection
Unnecessary Context:
I want to write a function that closes open HTML tags, because closetag.vim seems complicated, and I like to understand all the stuff I run in Vim.
My plan for this is to match ...
2
votes
3
answers
79
views
How to avoid highlighting when block commenting?
I'm running the command
:.,.5s/^/!&
to block comment a section of code, however, after running the command the editor highlights the first character in every line of the file. I guess this doesn'...
12
votes
1
answer
34k
views
Search for lines starting with given string in Vim?
I have this code snippet:
# import ipdb; ipdb.set_trace()
def foo():
...
import ipdb; ipdb.set_trace()
...
# import ipdb; ipdb.set_trace()
How should I specify search query, that it ...
1
vote
0
answers
324
views
Matchit Plugin: issues when resetting regular expressions in "b:match_words" for detecting Matlab file keywords
I'm trying to implement a custom regexp string to be used by matchit.vim for identifying matching keywords that can be jumped between with %.
I found a utility from FileExchange (in the ftplugin ...
0
votes
2
answers
201
views
How to replace spaces inside a selection
I have this text :
He's a self made man who gets what he wants.
The small blue forget me not flower was first used by the Grand
Lodge.
And this function:
function MyFunction()
let myList1 ...
1
vote
1
answer
34
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 ...