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 votes
1 answer
32 views

strings that have two 'a' ,between position 3th and 10th [closed]

I have a question in Regex. I want to find the strings that their length is not more than 40 and between thier 3th character and their 10th character have exactly two a. example: mmaammm mmmaammm ...
2 votes
1 answer
71 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
18 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 ...
  • 35
2 votes
1 answer
65 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 ...
  • 35
0 votes
2 answers
64 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
219 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
25 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 ...
  • 479
2 votes
1 answer
50 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 ...
  • 21
0 votes
2 answers
30 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
43 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 ...
  • 167
0 votes
2 answers
30 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
21 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
40 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
54 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 ...
2 votes
0 answers
40 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:[...
  • 121
1 vote
1 answer
61 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
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). ...
1 vote
1 answer
37 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
73 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
63 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
65 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
66 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
117 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 ...
  • 143
0 votes
2 answers
260 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
0 answers
16 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() {} } ...
  • 145
0 votes
1 answer
113 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
35 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
26 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
495 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
0 votes
1 answer
324 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 \...
  • 135
3 votes
1 answer
417 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{...
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 ...
  • 1,277
3 votes
1 answer
76 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: ...
3 votes
2 answers
433 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: /...
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....
3 votes
1 answer
293 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 ...
  • 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 ...
0 votes
0 answers
66 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 ...
  • 121
0 votes
1 answer
200 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 ...
0 votes
1 answer
42 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 ...
1 vote
1 answer
110 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.
2 votes
1 answer
57 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 ...
1 vote
1 answer
41 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 * ...
4 votes
1 answer
147 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 ().
  • 381
0 votes
2 answers
73 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 ...
  • 645
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 ...
's user avatar
2 votes
2 answers
84 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 ...
  • 25
0 votes
1 answer
350 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 ...
2 votes
1 answer
47 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 ...

1
2 3 4 5
11