Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

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
1 answer
25 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 ...
user avatar
1 vote
0 answers
16 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). ...
user avatar
1 vote
1 answer
29 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> ...
user avatar
-2 votes
1 answer
27 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 /...
user avatar
1 vote
2 answers
67 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 !&...
user avatar
1 vote
1 answer
34 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 [...
user avatar
1 vote
1 answer
35 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 ...
user avatar
0 votes
1 answer
50 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 ...
user avatar
4 votes
2 answers
111 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 ...
user avatar
  • 143
0 votes
2 answers
70 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= {...
user avatar
  • 151
0 votes
0 answers
15 views

How to use advanced Regex features without too many syntax cluster

I have a bunch of typescript classes in a single file, each of them has an _update function. I want to go to _update function of a class Player. How would I do that? class Player { _update() {} } ...
user avatar
  • 145
0 votes
1 answer
87 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 ...
user avatar
1 vote
1 answer
33 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 ...
user avatar
  • 137
1 vote
1 answer
20 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 ....
user avatar
  • 133
0 votes
4 answers
275 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 ...
user avatar
  • 199
0 votes
1 answer
261 views

How to delete all lines that start with a `\`?

Here is (part of) a file that I am working on turn down\ turn off\ turn on\ \pard\pardeftab720\ri-6\sl-660\qc\partightenfactor0 \f0\b\fs32 \cf0 U\ \pard\pardeftab720\ri-6\sl-240\qj\partightenfactor0 \...
user avatar
  • 135
3 votes
1 answer
236 views

Match one regex pattern if not preceded by another one

I'm trying to match \item \macro{this\var}.% YYYY-MM-01 where 01 can be 1 and replace with \item The Month is MM \item \macro{this\var}.% YYYY-MM-01 when NOT this case: \item MonthYY \item \macro{...
user avatar
2 votes
2 answers
34 views

How do I get substitute() function to work on individual lines like s///g

When I select a range visually and run this command: :'<,'>s/\(.*\)\s\+#.*$/\1/g It removes the text at the end of each line that starts with # which is what I want. I want to run a vim function ...
user avatar
  • 1,237
3 votes
1 answer
52 views

Capitalizing a backreference in a search (NOT a substitute)

I am looking for a way to match any line in a csv file that contains an identifier (e.g. OfficeLocation) and then, later on, the same identifier in all caps. A line to be matched might look like: ...
user avatar
3 votes
2 answers
430 views

How do I replace single whitespaces without affecting multi-whitespace such as indentation in Vim?

I have textfiles with paths that have lost underscores between words inline in an OCR process. I want to keep the indentation and put back the underscores in the paths. I have a path like this: /...
user avatar
0 votes
1 answer
22 views

Replace interpolated variables in Python string with positional formatting strings

How do I, with one regex, transform: logger.info(f"Foo {alpha}") logger.info(f"Foo {alpha} bar {beta}") logger.info(f"Foo {alpha} bar {beta} baz {gamma}") Into: logger....
user avatar
3 votes
1 answer
136 views

Delete all lines not matching a multi-line selection

I'm aware that :g/<body/ .,/body>/d can be used to delete everything within the multi-line selection betweeh <body and body>. I'm also aware that :g!/<pattern>/d can be used to ...
user avatar
  • 133
0 votes
0 answers
14 views

Storing Latex Chunks in Registers

Previously I asked about how one could yank both sides of an equality into registers. My use case was writing latex and wanting to be able to take work that I previously just wrote and modify it ...
user avatar
0 votes
0 answers
50 views

Regex multiple lines search, matched, but highlighted only what initially is on screen

Let's say I have the following fantasy code // a long definition of a structure struct long_struct { ... }; There are tons of multiple lines inside such block of code. It cannot be displayed ...
user avatar
  • 121
0 votes
1 answer
131 views

How do I sort lines after a prefix?

I would like to sort all lines in a file based on where the text of the line starts, after a line index which is printed into the file, like this: 126. This is the start of the line. It was ...
user avatar
0 votes
1 answer
41 views

How can I connect separated lines without highlighting in visual mode?

I would like to connect broken lines, like: This is a line that continues. but instead of highlighting them with visual mode and connecting with Shift-J, I would like to run some kind of global ...
user avatar
1 vote
1 answer
95 views

How do I enter N carriage returns during a substitution?

When using :s, how do I multiply the string being written by an integer, to write N copies of it? For example, :s/pattern/10*\r/ would write 10 carriage returns in each match.
user avatar
2 votes
1 answer
48 views

How do I replace all newline sections of any length with a single newline?

I would like to replace every section of newlines - for example, a single newline at the end of a line, or 5 newlines at the end of a line until the next line of text - with a single newline. I tried ...
user avatar
1 vote
1 answer
37 views

Why is asterisk not highlighting repeated occurences of a character?

I am using the latest version of Gvim on Windows 10. When I type / and some expression, for example /a, I see all matches in the document highlighted. The documentation says that the asterisk symbol * ...
user avatar
4 votes
1 answer
98 views

Character class (square brackets) ignored while matching regex

Why []([a].*) matches [](b)? I would expect it to require a to be the first character in ().
user avatar
  • 371
0 votes
2 answers
49 views

Illegal back reference when trying to delete \1

I have a bunch of expressions (matlab anonymous functions) of the form, for example `f\^I = @(x,y,z) x^y + z`, `g1\^I\^I = @(z,b,c) z^b + c` (where \^I means a tab, but I can't just write that in ...
user avatar
  • 625
2 votes
1 answer
80 views

Remove duplicate spaces except those belonging to initial indentation?

Summary: How to remove duplicated spaces in visual selection and/or globally while preserving the indentation? Imagine you have a mess like this: line of text indented line of text and ...
user avatar
2 votes
2 answers
78 views

Divide a line with curly braces that contains curly braces

The input line contains pairs of curly brace and in each pair of curly braces there might also be curly braces in the contents. Multiple layers of curly braces may also happen. Here's some example ...
user avatar
  • 25
0 votes
1 answer
178 views

Regex for all spaces that not surrounded by quotes

I'm trying to write a regex that matches all spaces except those enclosed in quotes. I found solutions on the Internet for various programming languages, for example, here, but I am having trouble ...
user avatar
2 votes
1 answer
46 views

What is the difference between character classes \d and [:digit:] in a regular expression?

I studied a tutorial for vim syntax files and found this example: " Floating point like number with E and no decimal point (+,-) syn match celNumber '[-+]\=\d[[:digit:]]*[eE][\-+]\=\d\+' I was ...
user avatar
1 vote
1 answer
29 views

Why did \a match some non alphabetic characters

As my understanding, in the regex of Vim, \a matches any of alphabetic character. Now I'm trying to change the line file_exts = [tex, log, aux,] into file_exts = [(tex), (log), (aux),] I've tried to ...
user avatar
  • 873
0 votes
0 answers
27 views

Matching patterns in autocommands

I'm trying to match a particular path pattern for an autocommand. I have this: au BufRead *.md if expand('<amatch>') =~ "/path/to/dir/[0-9]" It successfully matches, eg. c:\soemthing-...
user avatar
  • 21
0 votes
0 answers
28 views

Insert newline (\n) at search and replace but get "^@" instead [duplicate]

I want to set a linebreak after all braces }. I tried to replace the braces by newline \n with: :%s/\}/\n/ But all I got is a ^@ instead of a new line. Example: abc{def} foo bar{baz} and so on I ...
user avatar
  • 145
0 votes
1 answer
110 views

Using "(?=<)" in regex leads to "E866: (NFA regexp) Misplaced ?"

The following command should output the string "st": :echo matchstr('test', '\(\?<=e\).*') However, it leads to the error mentioned in the title. Reading :h E866 didn't help much. I also ...
user avatar
1 vote
1 answer
27 views

replace simple pattern with sequential next line from another file

My file working.txt has 1000's of lines, which repeat in a simple pattern as follows: name date_ax name date_cd name date_ed name date_dc ...etc I need to replace each instance of name with the ...
user avatar
  • 531
2 votes
1 answer
62 views

Moving by word (w/b) and staying within the line

Background I'm attempting to remap the arrow keys to move by word while staying within the current line. I.e. I'm looking for a behavior similar to w and b, but that acts like h and l in the sense ...
user avatar
0 votes
1 answer
50 views

How to create an abbreviation that matches a regular expression

I'd like to create an abbreviation that will change the following (in a markdown file): - list item 1 into this: - list item 1 Whenever I type - f it should be expanded to - f (from 1 space to 3 ...
user avatar
  • 153
0 votes
1 answer
29 views

How to put sync region start=<this> into a variable?

I have the following code: com! -nargs=* ShFoldFunctions <args> fold ShFoldFunctions syn region shFunctionOne start="^\s*[A-Za-z_0-9:,][-a-zA-Z_0-9:]*\s*()\_s*{" end="}&...
user avatar
  • 173
1 vote
1 answer
34 views

How do I do substitution against just part of a larger pattern?

I have a string like 18NTC101L , which I can search by \v[0-9]{2}[A-Z]{3}[0-9]{3}L. Now, I just want to replace the L at the end with letter T. How I can do that?
user avatar
  • 387
1 vote
1 answer
90 views

Insert and remove a marker at the start of the current line

So, I'm trying to set up a sort of bookmark/marker kind of workflow, and I came to notice I'm used to put characters such as # and such for different meaning inside my own notes, etc. Now, I already ...
user avatar
0 votes
1 answer
128 views

Add or remove single quotes around numbers in yaml files with vim [duplicate]

I'm editing some large YAML files with neovim, and need to add (and remove, in some cases) single quotes around some values. For example - code: 1234 needs to be changed to code: '1234' in other cases,...
user avatar
  • 1
1 vote
1 answer
135 views

Why does /^[\s]* not work how I expect?

I use vim as my $MANPAGER. I will often use :/^[ ]*-F (for example) to quickly jump to a flag and read up on it. However, the equivalent :/^[\s]*-F doesn't return the same results. Why not?
user avatar
  • 199
6 votes
1 answer
238 views

Matching any Unicode whitespace

I'd like to match in Vim any whitespace character, not only normal space (U+0020) and tab (U+0009) with \s, and the other ASCII ones like form feed (U+000C) with [[:space:]], but any Unicode ...
user avatar
  • 161
0 votes
1 answer
31 views

Can't replace div using regex

I have the following code from lines 6-20 <div> 1 </div> <div> 2 </div> <div> ...
user avatar
1 vote
0 answers
145 views

Bulk file rename in Vim Visual Block Mode

I am using VIM Renamer plugin to bulk rename files, I want to replace E008_r5_Haz_Area_Class NAT GAS.pdf E009_r4_Haz_Area_Class.pdf E010_r4_Haz_Area_Class MP STEAM.pdf E011_r3_Haz_Area_Class.pdf ...
user avatar
  • 11

1
2 3 4 5
10