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
0 votes
1 answer
34 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
32 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,}\)...
  • 1,022
2 votes
2 answers
536 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,022
1 vote
1 answer
74 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 ...
  • 1,022
0 votes
1 answer
20 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
30 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 ...
  • 381
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=...
  • 360
0 votes
1 answer
29 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 ...
  • 145
1 vote
1 answer
47 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
30 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"...
  • 1,972
0 votes
1 answer
17 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: ...
  • 2,014
0 votes
1 answer
109 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 ...
  • 37
0 votes
2 answers
58 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
23 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, ...
  • 1,739
0 votes
2 answers
58 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 ...
  • 103
2 votes
3 answers
308 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 ...
  • 441
0 votes
2 answers
124 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', ...
  • 37
1 vote
2 answers
66 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
128 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?
  • 261
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
  • 261
1 vote
1 answer
215 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 ...
  • 113
2 votes
1 answer
44 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 ...
  • 33
2 votes
1 answer
192 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
  • 143
1 vote
1 answer
27 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 ...
  • 37
2 votes
1 answer
846 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 ...
  • 37
0 votes
3 answers
91 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 ...
  • 1
4 votes
1 answer
235 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 ...
  • 533
4 votes
1 answer
206 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 ...
  • 41
0 votes
2 answers
49 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
68 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 ...
  • 187
0 votes
2 answers
35 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
63 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
176 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
66 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
93 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:[...
  • 131
1 vote
1 answer
282 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
50 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
75 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
138 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
130 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
120 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 ...
4 votes
2 answers
118 views

Replace only after specific pattern

I have lines like this: $params["request_id_$i"] => $v["request_id_$i"]; $params["drug_name_$i"] => $v["drug_name"]; I want to replace all _ occuring ...
  • 145
0 votes
2 answers
628 views

Why does in a regular expression \zs \ze cause "E486: Pattern not found"?

I want to find and eventually delete all whitespace between all occurrences of an equal sign and the next character. For example, turn @article{article1, author= {author1}, journal= {...
  • 161
0 votes
1 answer
325 views

VIM search and replace regex - how to keep the search string intact and adding to it?

I'd like to learn how to search and replace in VIM - keeping the search string intact and adding to it. Take this as a simple example. See image below. I copied a simple python script written for ...
1 vote
1 answer
47 views

What pattern should I use for word-like movement?

Given a line of text like the following let FindTargetsByRegexp = { re -> {lnums -> s:_FindTargetsByRegexp(re, lnums) }} I want the expression \(\n\|[^A-Za-z]\)\zs\S to stop on the same word ...
  • 137
1 vote
1 answer
33 views

highlighting: match dot in group

I have several syntax match patterns, that logically belong together. Currently I match them like this: syntax match FOO /\v<(foo1|foo2|foo3)>/ What I actually want to match is .foo1, .foo2 and ....
  • 133
0 votes
4 answers
2k views

How to "match until"?

I have a CSV filled with text messages, and I want to be able to easily filter by sender/receiver. The last field indicates "SMS", "iMessage" or "WhatsApp"; the previous ...
  • 209

1
2 3 4 5
11