We’re rewarding the question askers & reputations are being recalculated! Read more.

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
-2
votes
0answers
20 views

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

tester.84tester:tester -> tester.84tester:tester84 this is what I need to be done, thank you for any help.
2
votes
2answers
168 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 ...
6
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 \...
1
vote
1answer
56 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 ...
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 ...
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 ...
3
votes
2answers
107 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=.*\)\@...
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
56 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 ...
2
votes
1answer
75 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
1answer
71 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
249 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
1answer
81 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 ...
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 ...
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
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
47 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
50 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 ...
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 ...
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 ...
-1
votes
2answers
49 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
35 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 ...
6
votes
3answers
441 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 ...
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
38 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
174 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 (...
5
votes
4answers
229 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"),(...
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
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 ...
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, ...
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 ...
2
votes
1answer
70 views

How to match file pattern, with extension, not containing a specific word?

I'm trying to use autocmd BufNewFile to like so: autocmd BufNewFile \(.*_spec\)\@!.rb 0r ~/vim/skeleton.rb autocmd BufNewFile *_spec.rb 0r ~/vim/skeleton_spec.rb So, I want the first ...
1
vote
1answer
31 views

Rearrange c++ const and reference specifiers

I'm trying to convert const std::string &s to std::string const & s and can't seem to get it quite right. This is the regex I'm using: %s/const \(.\{-}\) &/\1 const & and the ...
1
vote
1answer
340 views

How to search for any repeating character, X number of times

Let's say a buffer has a certain characters I know will be repeated seven (7) times. How can I search for any character repeated seven times? I know I can search for .., but those to characters won'...
0
votes
0answers
30 views

Search and replace any number separated by : trailing with number or alphabet separated by . and / or - or _

Search pattern examples "0:4.10.5-5.el7" "0:0.7.7-13.20120626.ov7" "6:4.14.8-6.po7_3" How to search and print all occurrences like above patterns where the first number can be any number ranging ...
2
votes
1answer
35 views

(Dynamical) quantifier in search and replace

In VIM, using search and replace like :%s/pat/repl/g, is it possible to have a quantifier in the replace part? As an example, replace every line matching pat by exactly 100 # without actually typing ...
0
votes
0answers
32 views

How to pass '\v\C[pattern]' into the built-in match() VimScript function

I am attempting custom fold logic as my first VimScript. What works for me: USING VERY MAGIC (\v) and explicit CASE SENSITIVE (\C), find python comments in foo.py that begin with hash, then Roman ...
2
votes
4answers
88 views

Search for line, move to end of search string, then search+replace

(EDIT: In the original question, I had accidentally used \w instead of \<. Comments and answers with \w should be interpreted as using \< as well.) In my LaTeX source files, section headings ...
1
vote
2answers
51 views

Search and replace only if following character is “x”

I'm trying to make a search and replace to format the following type of strings. INPUT someName0 : in std_logic; someName1 : in integer; someName2 : out unsigned; someName3 : out std_logic_vector(3 ...
2
votes
1answer
75 views

Using (neo)vim's regex to match up to but *excluding* a certain character?

I have a quick question about the regex (neo)vim uses. After long hours of toiling, staring at documentation and trying everything else I could think of, I seem to be stuck. What I'm trying to ...
1
vote
0answers
225 views

Basic and extended regular expression [closed]

I have a question: How to search a string using a basic regular expression in Vi editor? How about using extended regular expression in Vi editor?. Is it a real difference between basic regular ...
1
vote
1answer
80 views

How to match each occurrence per line?

I have similar text on more lines: _AB_ _ABC_ _ OR _ _A_ _ABC_ I need to replace only the underscores that delimit some text. So I need: =AB= =ABC= _ OR _ =A= =ABC= How to match each pattern?
0
votes
1answer
31 views

Using submatches inside and outside of system() call

I have this substitution, which does not work: exe 's/<<\(.*\)\%(,.*\)\?>>/<<\1,'.system('ark headings \1 | head -1').'>>' What I want to do is: capture some expression ...
3
votes
1answer
55 views

Vim RegEx question: how to detect headings of markdown(.md) i.e. lines composed of '-' or '='

I'm learning Vimscript in Learn Vimscript the Hard way and trying an exercise. Here is this: The lines "underlined" with = characters are treated as headings by Markdown. :onoremap ih :<c-u>...