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
3answers
73 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 ...
2
votes
1answer
50 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
44 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
204 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
118 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
200 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
75 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
132 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
89 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
4k 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
617 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
179 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
54 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
33 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
39 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
69 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
41 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
5k 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
50 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
233 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
36 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
37 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 ...
1
vote
1answer
51 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
28 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
162 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
3answers
193 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
23 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
90 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
44 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
19 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
45 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, ...
24
votes
3answers
62k 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: ##############################...
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
328 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
42 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
51 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
29 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 ...
2
votes
4answers
86 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
1answer
89 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
29 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
28 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 ...
1
vote
2answers
49 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
59 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
158 views

Basic and extended regular expression

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 ...
3
votes
1answer
96 views

Parsing a valid normal-mode command (possibly with regex)? (for Vim-like Word add-in)

I am writing a VBA routine so I can use normal-mode commands in Microsoft Word. (Yes, I am! Edit And here you go!) I am only going to support the d, c, and y operators initially, and only char/word/...
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
29 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 ...