Questions tagged [regular-expression]
Regular expressions (often shortened to "regex") are expressions written in a declarative language used for matching patterns within strings.
535
questions
0
votes
1
answer
34
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
23
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
34
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
17
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
73
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
61
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
37
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
42
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
591
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
83
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
25
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
39
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
43
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
30
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
82
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\ '!....
1
vote
0
answers
58
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
19
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
213
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
127
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
30
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
63
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
323
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
142
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
67
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
141
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
3k
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
309
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
53
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
282
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
28
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
1k
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
96
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
240
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
26
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 ...
4
votes
1
answer
358
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
2
answers
66
views
How do I copy and paste the content inside quotes from one place to another?
Lets say I have a buffer with the following content:
<property name="sfgiusfgsnffgdsb" value="arbitraryvalue1"/>
<property name="sfsergggsnfgdfgs" value="...
1
vote
1
answer
78
views
Vim: reformat CSS from one-line to multi-line rules
I wanted to reformat the W3.CSS file to make it more readable and study it.
I looked for an easy way to do this but couldn't find it (in vim-only way, obviously, the web are plenty of solutions and ...
0
votes
2
answers
37
views
Neovim wont match beginning of word with # sign
/\< matches beginning of all words as expected.
/\<a matches beginning of all words starting with a
but /\<# doesn't match words starting with #
what am I missing here?
Was planning on using ...
0
votes
1
answer
109
views
vi regex no match
I have this
os.environ.get("DB_NAME")
and I am trying to use the vi regex command
% s/os\.environ\.get\("DB_NAME"\)/db_name/gc
to match that. I used it to change flaskdb to os....
2
votes
3
answers
270
views
Capitalize a match in search and replace
I am using the following regex to find the first letter of the last word on each line in a file:
%s/.* .\zs/
I want to now lower case this match on every line in a file. How do I do it?
For eg.
Before:...
0
votes
1
answer
73
views
How to join multiple lines which doesn't include space at the end
I'm working on a subtitle document (.srt) which has thousand of lines. But this issue is not about the subtitle documents, I going to export as plain text to read after. To see the dialogs line by ...
3
votes
0
answers
139
views
How to replace a string not followed by a / character?
How do I replace all occurences of a string that are not followed by a forward slash character in vi (or vim)? I want all occurences of '<[text]', but NOT '</[text]' to be replaced with '<db:[...
1
vote
1
answer
339
views
How can I remove duplicates in comma-separated text, ignoring the first column, keeping only unique lines?
I have a sparse CSV file with data shaped like in this simplified example. I am omitting the header, Age,Fruit,Color,Count, but I can include it in my file if needed. You can see that the sparse rows ...
1
vote
0
answers
17
views
Search and replace with regex in the replacement [duplicate]
I would like to replace all instances of match("something") with match("something", source). However, using
:%s/match(.*)/match(.*,\ source)/g
replaces it by match(.*, source). ...
1
vote
1
answer
53
views
"non-greedy" [<any chars>](word) regexp problem
consider this sentence (a line in a text file):
mi chiamo [Giorgio Robino](person) ed abito in [corso Magenta 35/4 a Genova](address)
I want to search (select/extract) the pattern <any chars> ...
-2
votes
1
answer
28
views
I want to get the Use% values from this individually. I used \d+% to get the values having %
I would like to match percentage in the next lines:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 1503280 1388392 20476 99% /
/dev/mmcblk0p7 2753568 4616 2586036 1% /var/lib/docker
/...
1
vote
2
answers
77
views
How to make substituition of pattern if it occurs after different pattern?
I would like to have a regular expression that replace:
"foo" "bar" Is "hello" "world" "!"
by:
"foo" "bar" Is "hello world !&...
2
votes
1
answer
202
views
How to select a regexp group in vim?
I want to select/visualize a regexp GROUP in a vim regexp expression in vim.
By example I would like to highlight (search) a span of characters inside square brackets, having this line:
bla bla bla [...
1
vote
1
answer
145
views
Ultisnips not working with space
I have the following snippet
context "math()"
snippet `v([a-zA-Z]) ` "bb" A
``rv = '\\mathbf{' + m[1] + '}'``
endsnippet
Now, most of my snippets work, however, snippets with ...
1
vote
1
answer
135
views
How do I use the \+ quantifier?
Running Windows 10 (Home) and
VIM - Vi IMproved 8.1 (2018 May 18, compiled May 18 2018 18:36:07)
MS-Windows 32-bit GUI version with OLE support
Included patches: 1
Compiled by mool@tororo.
I have ...