Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

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
2answers
25 views

Why do the \| characters in an :execute “normal …” mapping require extra escaping backslashes?

I'm struggling to understand a character escaping situation involving \|. First I'm running the following search which matches either the A character or B character. I'm using the default magic ...
7
votes
6answers
2k views

How to replace a pair of brackets

I have a file, containing some lines like this: aaa bbb (ccc) ddd. Now I want to replace the pair of brackets by a pair of square brackets to make this line become aaa bbb [ccc] ddd. I think that \...
-2
votes
0answers
21 views

Numbers transferred from beginning to the end of a string REGEX [closed]

tester.84tester:tester -> tester.84tester:tester84 this is what I need to be done, thank you for any help.
5
votes
4answers
231 views

How to get all distinct words within a set of lines?

I would like to extract a list of distinct words from a set of lines. Is there a way of doing this ? Say for example I have lines that look like this: [ [(isPhysicallySettledFxFwd, NO,"Y"),(...
2
votes
2answers
173 views

How to use lookahead and lookbehind in vim in the same line

I am trying to delete all the spaces that aren't sandwiched by a letter. So this I am trying; but ; I can't find a way ; should be this I am trying;but;I can't find a way; I tried this but it ...
1
vote
2answers
108 views

How to replace a space with a colon - command line mode

Input Every line is separated into 8 parts by semicolons Every part can include words,numbers,space etc. Output I am trying to replace the space in the 6th part with a semicolon, thus separating ...
1
vote
1answer
83 views

Substitute at begining and end of a pattern (not line by line)

Is there a way to make a substitution only at the beginning and end of a pattern and NOT line by line? E.g., searching for the pattern /^\*\{2,}.* in * Micro topic 1 ** Microbes are small ** You can'...
11
votes
3answers
971 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 preceding atom. But I don'...
1
vote
1answer
63 views

How to paste text after each line matching a pattern?

I have a file like this: int var; int f() { // Use of var } int g() { // Use of var } int h() { // Use of var } // ... I want to move my global var into each function to get this ...
2
votes
1answer
78 views

Named capture groups in vim regex?

Does vim allow us to assign names to capture groups for search/replace that can be used in place of \1, \2 etc... for back-references and substitution? Like in in Python: >>> import re >...
3
votes
2answers
110 views

Replace after a specific pattern

Suppose I have a string as follows: RESULT=100 How would I replace everything following 'RESULT=' so that the string reads: RESULT=200 The closest I've gotten so far is to use: :%s/\(RESULT=.*\)\@...
1
vote
1answer
57 views

How to match a word not following a defined pattern?

I have the following text file: 1: This is a book. 2: Here is the book for you. 3: There are book. 4: You can read the book. 5: Is book good to read? I want to find all occurrences of book not ...
25
votes
3answers
66k views

Find and replace using regular expressions

I have a file with a bunch of user defaults in. I want to change some of the text, but I'm struggling coming up with a matcher and replacer. Using the following example: ##############################...
1
vote
1answer
27 views

Adding prefix and postfix to matching regular expression

Dear fellow Developers, I would like to quote variables having @ inside like test@mail.org, but not starting with @ like @var1, @var2. Does Vi/Vim/Neovim have any option to inspect the matched pattern ...
10
votes
6answers
1k 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 ...
2
votes
1answer
57 views

Change to standard regex syntax?

This might rustle some feathers, but I do not like how vim's regular expression syntax differs from standard regex syntax (I've read this answer, but I don't think this is worth remember two syntaxes ...
0
votes
3answers
83 views

VIM Macro to delete all Perl POD?

POD is a markup format that is used by Perl. It's sometimes found embedded within the code. You can find more information on it from the official docs on perlpod, and perlpodspec. I'm wondering if ...
3
votes
1answer
72 views

When searching for the string under the cursor, how does an atomic group prevent a match outside a string?

TLDR I have this text in a buffer: a 'string' and 'another one' ! ^ cursor is here And I search for this regex in a / command: \%(.*\%#\)\@=\%('[^']*'\)\@>\%(\%#.\+\)\@&...
3
votes
2answers
254 views

Regex pattern matching cursor-position

Considering the following regex pattern: /\s*<<.\{-}>>/ I want to match those patterns only when the cursor is somewhere on this pattern. So, if have the line (the character enclosed in ...
1
vote
3answers
134 views

Add space after parens

My Problem I am editing a markdown file with some links in a [link text](url) format. Some of these links lack a space after the closing parens, e.g.: ...fit the [Single Responsibility Principle](...
0
votes
2answers
241 views

Highlighting a search capture group

By :set hl, the search result of a / expression is highlighted, for example: And if one wants to replace one substring distinguished by its surrounding, one can do so elegantly using regex capture ...
1
vote
1answer
82 views

Regex to change align into equation

I am trying to correct some long latex files > 10 000 lines. Something that bothers me and messes with the typopgraphy is the missuse of align and equation environments. The equation environment ...
1
vote
1answer
150 views

How to visually select a pattern only on current line using vimscript?

I'm trying to write a vimscript function that will allow me to map a key to select a regex match on the line my cursor is on. This will help with selecting strings for use with ctags. For example, I ...
3
votes
1answer
92 views

regex that requires that two matches have the same length

I'd like to find a regex that matches Setex-style headings like: My Heading ========== The headings text and the underline need to be of the same length. However, I don't know how to specify a regex ...
17
votes
5answers
5k views

Vim syntax pattern to highlight python keyword argument

I'm struggling with learning vim regexps (I don't know any other regexp very well either), so the main problem is to define regexp that would match some_function_call(simple_value, keyword=value) ...
6
votes
4answers
673 views

Store the number of matches in VimScript function?

Is there any way to store the number of matches in a variable in a VIMScript function? For instance I'm using: %s/,//gn
6
votes
2answers
213 views

How to search literally without any regex pattern?

Does vim allow for searching raw strings? If what I want to search is in the variable string, does the following code work? call search('\V' . escape(string, '\')) Or are there other more direct ...
0
votes
0answers
36 views

Regex match inside XML container

I'm trying to match data in a gnucash record, without skipping into the next record, so basically needing to stop matching at the closing tag. The record looks thus, for example: <trn:split> ...
0
votes
2answers
56 views

Finding Markdown image tags with regular expressions (E486)

I have a document that contains lines like ![My title](/path/to/image.jpg) This is the format for images in Markdown. I have constructed a regular expression which I believe should be able to find ...
0
votes
1answer
35 views

Yank both sides of equation

While writing in latex I often want to copy a math equation's right hand side or left hand side. For example I have something like this \begin{gather*} 5 \cdot \left( A A 3 \right) + 50 \cdot \...
0
votes
1answer
48 views

What alternative to capture groups should I use in VimScript?

I understand that Vim does not support regex capture groups. The problem I am trying to solve is being able to easily capture parts of lines. For example, given the following line of python source ...
1
vote
3answers
72 views

Search for line and paste as column until next match

I'm wondering if this is possible, more for curiosity's sake, but also it might be useful to know. I have several ascii data tables and I want to combine them all into a single file. So I combined ...
0
votes
0answers
53 views

can not make this regex pattern work in “very magic” mode

I am trying to construct a regex pattern to match characters inside two # characters. For example: #abc#bc#cd# I would like to match abc, bc and cd. The following regex without using magic mode ...
14
votes
3answers
6k views

How to make regex matchers non-greedy?

I am trying to use a regex for replacing text in a file (replace a full url with just protocol/domain/): :%s/\(https\?:\/\/.*?\/\).*/\1/gc Unfortunately .*? does not match the string, even trying to ...
3
votes
1answer
55 views

How to replace the first matching for each line with regular expression

Saying that I have such a file as below: <random_string>hello<random_string> random_string<random_string>hello<random_string> I want to remove the first square bracket and ...
6
votes
3answers
472 views

regular expression to find lines containing multiple specific words or patterns in any arbitrary order

Suppose we have the following very simple file: 1 x 3 x x 3 x 1 Now I'd like to have a pattern that matches all lines containing both 1 and 3. The order and position should not matter. (Note that I ...
-1
votes
2answers
53 views

Increase numbers by one in a visual block

I edit DocBook XML files. In my plugin, i want to implement a function that would increase or decrease the section numbers recursively, assuming that cursor is at the toplevel section. For example, ...
0
votes
0answers
36 views

Regex to remove newline; works in one vim instance, but not another

Trying to remove newlines with a regular expression (not shift-j). I have one vim install where :g/conf/s/\n// works as intended, removing a newline from all lines containing 'conf'. I have a ...
0
votes
1answer
40 views

Why doesn't this `:execute` command work?

I was looking at this question on Unix & Linux Stack Exchange, and I thought I could write an answer for it using Vim. I ran the following for a quick test, and was surprised to find it didn't ...
2
votes
1answer
56 views

Substituting (*x).y with x->y

I am coding a C project and as I am unexperienced, everytime I needed to access the element of a pointer to a struct I typed (*x).y (where x is a struct and y an item), which I just realized could be ...
0
votes
2answers
39 views

Can capture groups be used like perl?

I'm trying to determine how many # characters are at the beginning of a line. In perl, I can so something like this: $match =~ m/^(#+)[^#])/; $length = length $1; Trying to do something similar in ...
1
vote
4answers
175 views

In Vim ex mode how to re-use a string from the default buffer in the command

I'm trying to modify this file, by extracting the very first line in the file and prepend it to each of the consecutive lines until end of the file black widow card Friday berry I used ex command (...
0
votes
1answer
24 views

Why does backslash-escaping a CJK character cause a very-magic regex match to fail?

My understanding of very-magic regexes is that you can escape any character outside [A-Za-z0-9_] with a backslash to make it match literally. However, I don't see that behaviour with CJK characters. ...
2
votes
1answer
124 views

How can I search for Unicode characters by hex code in very magic regexes in vimscript?

I am working on UTF-8 support for my editorconfig-core vimscript plugin. I have hit a strange snag. This is in official GVim 7.4 Win32. I would like to get this working on 7.4, rather than ...
1
vote
0answers
58 views

custom fold expression to fold functions

I would like to create a custom fold expression for functions of the form x <- function(a,b,c) { ... ... body ... } where the lines starting from function(...) until the closing brace are ...
0
votes
1answer
21 views

Can you run a regex on a register or the clipboard contents?

I want to copy something to a register or more ideally the system clipboard and then run a search / replace regex on it the way that I do in the buffer...is that possible?
0
votes
1answer
52 views

Can you match unicode blocks by name in vim?

\p{Cyrillic} Works in my normal regular expressions. When trying to match in Vim or gvim however, I can not seem to get it to work. I tried something like this: \p\{Cyrillic} Is it not supported, ...
0
votes
1answer
1k views

E488: Trailing characters error in VI with find and replace command

I am trying a simple find and replace function in VI Editor to change a profile path but the / is getting in my way and I need a simple and clever solution. I tried using: %s/profile: "/fldr0/local/...
1
vote
2answers
418 views

Find and replace string with slash in it and retain the slash in the new string

I am having a tough time finding and replacing a string using vi. Source String: &DB2_PORT;/&DB2_DATA; New String after replace: &DB2_PORT;/&DB2_DATA;:tcp=true I can find &...
1
vote
1answer
43 views

How can I format a function declaration to force curly brace at the end of the line?

I have big portions of code where functions are formatted with the opening curly brace on the next line, like this : function foo() { } I'm trying to figure out how to search / replace in order to ...