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

find next comma, skipping anything in parens?

Suppose I want to create a macro that swaps two function parameters. For example, I search for swap: swap(first, second); And with the cursor pointing at the 's' in swap, I can search forward for ...
-2
votes
1answer
52 views

Search for " and replace with ' [closed]

The basic format I need it in is this. %s/search/replace/c So I want it to search the entire file and ask for confirmation for every match found.
1
vote
1answer
58 views

Substitute words for characters between matches in a line?

The raw text is like this: <words> command feed world </words> tag=random <words> who is that ? </words> tag=normal I need to substitute words for characters: <...
0
votes
1answer
92 views

Recognizing expression written in assembly containing $ and new line

I'm trying to remove instances of the following: add $t1, $zero, 9 sw $t1, 8($t4) Where 9 could be any number, however 8 is consistent. If I copy this expression in Vim I get: ^Iadd $t1, $zero, 9^M^...
0
votes
2answers
72 views

How to get command line :g to output pattern search result including the first line?

When I use the normal mode and output the search result using the following input, /LOAD \d\{-}\_p\+\d\n it includes the first line, However when I use the ex-command, :g/LOAD \d\{-}\_p\+\d\n/z ...
1
vote
1answer
306 views

VIM regex - match all words equal to one under cursor, except one currently under the cursor

I need to create regex in VIM (for plugin purpose) which would work as follows: Expected behavior: || characters indicate current cursor position, bold words indicate matching words Example nr 1: ...
2
votes
2answers
423 views

Look ahead non greedily?

adfa.ad.asdf.efa.saf mark I need to match the saf mark, no more no less ahead. I find that if I use this, /\v[,.;]\zs.{-}mark It matches all along to the first .. Any suggestions? Thanks.
2
votes
1answer
417 views

How to add quantifiers to group in a pattern in VIM?

I am trying to highlight a set of lines, between the line containing ULS at the top of the line, to the line containing star in the bottom. I have tried many combination but I have failed. If someone ...
0
votes
2answers
4k views

How to regex everything that is enclosed between two certain strings in visual selection

I want to find the string enclosed by string1 and string2 and replace it with something else. I got a few lines that look something like some_string_and_string1_some_rndom_string_string2_and_more ...
0
votes
1answer
100 views

How do you sort lines by longest substring to shortest substring?

Here are some sample strings, sorted by longest string to shortest sting: "Bi(NO<sub>3</sub>)<sub>3</sub>·5H<sub>2</sub>O","bismuth nitrate pentahydrate"); "Be(C&...
0
votes
1answer
205 views

Substitute variable into match

This highlights the parts of the lines that are over 80 columns long: augroup over_length autocmd! autocmd BufEnter * highlight OverLength ctermbg=darkgrey autocmd BufEnter * match ...
1
vote
1answer
731 views

Replace numbers with white spaces

I would like to replace all floating point numbers within given block with white spaces so that the number of the latter corresponds to the "length" of the floating point number. For instance, I want ...
3
votes
2answers
1k views

How do I get the first letter of a captured group?

The old three lines: able bee must I want to change them to this in vim: able /home/words/a/able bee /home/words/b/bee must /home/words/m/must I knew using %s/^\(.*\)$/\1/home/wprds/\1 can add ...
4
votes
2answers
220 views

Get list of lines that begin with a pattern

I have two files written in php, both with a bunch of functions. I need to compare these function names with the other file, so I'm trying to get a list of all lines that begin with function. ...
4
votes
3answers
330 views

How can I quote text in a search regex?

I want to yank text from a window and search for it but the text contains characters not interpreted literally by the search command, e.g. a file path. Is there an easy way I can quote the yanked text ...
3
votes
1answer
288 views

How to negate substitution pattern?

I've the following sample INI file: Foo=1 Bar=1 Baz=1 Qux=1 and my goal is to set all options to 0 except Foo. To disable one selected line, I know that I can run: %s/Foo=\zs.*$/0/ Test command ...
2
votes
1answer
247 views

vim search for arbitrary combination of spaces and tabs

I'd like to construct a mapping that would replace a b with c where the gap between a and b could be zero or more spaces or zero or more tabs, but not zero of both. There have been several ...
2
votes
2answers
4k views

Search a string excluding the lines starting with another string

In a fortran code for example, there are lines like c write(6,*) "some text" write(6,*) "some other text" How would I search for lines having a write but exclude all lines starting with c or ...
1
vote
1answer
73 views

Finding all occurences of backslash and deleting twice before every occurence of them

I got some output code that has random backslashes, followed by a new line. For example: ( 1-z+z^11-z^12+z^22-z^23+z^33-z^34+z^37-z^38+z^40-z^41+z^43- z^45+2*z^46-2*z^47+z^49-z^50+z^52-z^53+2*z^55-2*...
2
votes
1answer
181 views

Replace pattern containing more than one arithmetic expressions

My problem is to search all expressions in a file containing: (Scenario in converting from fortran to C) Arr1 = Arr2[4:10] and replace with for(i = 0 ;i < 6; i++) Arr1[ i ] = Arr2[ i+3 ] //...
3
votes
2answers
158 views

Turning a set of vi regexp into a script

I have a number of multi million line sets of data to edit before using a program to analyse them. I have a set of regexp (23 of them) which do the work. Things like :%s/\n\n/\r to get rid of ...
1
vote
1answer
126 views

vim script replace until match

I am trying to match a block of fortran, say, foo i() ... End foo i with autocmd CursorMovedI *.f90 :call FixName("foo") where FixName is: " FixName: Change Subprogram name {{{1 function! ...
3
votes
2answers
410 views

Zero-or-one in backwards regex search

Given the document a ^ a (where ^ denotes the cursor position), the forward search /a\? succeeds, but the backward search ?a\? fails. Why?
4
votes
1answer
897 views

Search and replace, special pattern for indent (ex: newline = '\r')

I am cleaning up some minified css. I found this article helpful for adding a newline before/after a search match. This SO thread has a pretty exhaustive list of ways to indent with VIM, but it DOES ...
3
votes
1answer
211 views

Evaluating and rounding a file of numbers in scientific form

I have a file full of expressions like this: 0.100000000000000000000*10^(1) 0.000000000000000000000*10^(0) 0.911403977421716024142*10^(-15) 0.983917614423065751378*10^(-15) and I would like ...
2
votes
1answer
508 views

Why this regex expression matching lines with (not N) number of comma doesn't work with vim?

I'm trying to fix a csv file but the regex expression won't work. I want to jump to lines that have a number of commas that is different than 5. I first select lines with less than 5 so I press / and ...
2
votes
3answers
667 views

vim match the outest bracket

How can I match the outest bracket and replace it with $$ in vim? For example: {a+b>c} --> $a+b>c$ {a+{\int_a^bf(x)dx=\set{a+c}}} --> $a+{\int_a^bf(x)dx=\set{a+c}}$ {a+{b+c}+d} and {e+{...
1
vote
1answer
134 views

What is the vim native way to apply math over pattern?

I need to re-factor a line with a repeated pattern in a way to apply some math over numbers. For e.g. let's assume the following line of code: testpoints = [Point(1,2), Point(1,1), Point(1,2), Point(...
1
vote
2answers
128 views

Pattern in match in mapping in autocmd

Working on a more lightweight vim-endwise replacement (endwise breaks imap <expr> <CR> mappings). In the following text: def name<-cursor here it adds a newline and another line with ...
2
votes
2answers
157 views

How do I make regexes mutually contain each other?

I've written regexes for markdown for emphasis and bold as follows: syntax match PandocEmphasisMarker "\v(^|\s+)\zs_[^\n\t_ ]+([^_\n]|(\S_\S)|(\s_\S)|(\s+_\s+))+[^\n\t_ ]+_\ze($|\s+)" contains=...
0
votes
1answer
85 views

vimscript parse yaml header and output to mapping

My goal is to make a mapping, that depending on the value of type in the yaml header, will run a command sourcing a file named the same as the value of type. I have file containing a yaml header ...
0
votes
1answer
291 views

Changing the type of brackets

I have a document containing these types of strings: C(A,B) C(D,E) C(D, E) I am trying to replace every occurence of C(...) with \SC{...}, changing the C in front of the bracket and the brackets ...
4
votes
1answer
1k views

VIM syntax file, how to match capturing groups?

Lets say I have a line like the following in a vim syntax file: syntax match Emphasis "\v\s+_\zs[^_\n]+\ze_\s+" In this line I'd like to create multiple capture groups: the first underscore as "...
1
vote
4answers
1k views

How to batch replace a list of words in file?

Is there a way I can perform a batch-regex-replace using a list of words? Simplified example: List of words Apple Cabbage Turtle Text to search Bike 2 Turtle 5 Bike 3 Bike 5 Cabbage ...
1
vote
1answer
74 views

Replace base character, retaining combining characters

I have a vocalized Hebrew text; most letters are followed by one or more combining characters. I would like to replace some of the base characters, leaving the combining characters intact. It seems ...
4
votes
1answer
168 views

Don't expand `%` in external grep call

I try to search for {% amount in all files under the current directory. Using ag, this works fine: ag -Q '{% amount' (-Q specify to not use any regex, try to match text literally) I have this in ...
1
vote
1answer
161 views

How do I use regular expressions in ternary statements / keymappings?

I'd like to create a keymapping with a conditional statement, so that it behaves differently when a given line is blank. This thread gave me the following snippet, which is great – except that it ...
14
votes
1answer
512 views

Is there a way to convert a Vim regex literal to different magic modes?

If I have a magic Vim regex in a string literal, is there a way to convert the whole regex into an alternative representation that uses a different magic mode, so I could paste that equivalent regex ...
1
vote
1answer
57 views

Move the partly duplicated(repeated over lines in the middle part) lines to the end?

The text I want to process is like (after sorting regardless of the first column: sort/^[^|]*/): 236|c this is character ace|end but not the same i24|f string f is different|this is the end 235|this ...
0
votes
1answer
70 views

Search all mention of the current file's file name

I'm trying to add a simple command to search of rails partials files inside my rails app. I know I can get the filename without the extension with expand("%:r:r") but in my case because it's haml I ...
0
votes
1answer
133 views

Globally replace a particular pattern with another string

In VI I need to replace a pattern that looks like this: seq- followed by exactly 24 chars (can be 0-9, a-z, A-Z only) followed by / followed by some number of (0-9, a-z, A-Z). So an example string ...
16
votes
1answer
2k views

Why doesn't Vim regex allow more than 9 capture groups?

From :h E65 we can see that Vim doesn't allow more than 9 capture groups in a substitution command. For example the following command will work: s/\v(a)(b)(c)(d)(e)(f)(g)(h)(i)/\9\8\7\6\5\4\3\2\1 ...
1
vote
1answer
243 views

How can I make two substitution command in one line

Giving these 2 substitution commands in vim: %s/}$/\r}\r/g %s/^\s*([a-zA-Z\#\.\:\s\(\)-\[\]'=]*)\s*\{/$1 \{/g I have 2 questions: How can I join 2 substitution commands in a one line command ? Is ...
0
votes
1answer
685 views

Substitute words enclosed between comma or open-parenthesis and comma or closed parenthesis

I have lines like the following ones (actually function prototypes) void () word ( word, another word, one_more word, ..., hello, ... ) one argument ( only ) I want to match each single argument and ...
0
votes
1answer
5k views

Substituting something with empty string? [closed]

How can I substitute something with empty string? So far the best I can do seems to be substituting with a whitespace. If I write :s/target// then it's deemed invalid.
1
vote
1answer
69 views

onoremap: defining a bounded region

I want to craft a custom operator-pending motion if for functions in R language that look like this: doSomething <- function(arg1, arg2, ...) { doStuff(...) } The idea is to be able to press ...
13
votes
4answers
1k views

What is the rationale for \r and \n meaning different things in s command?

We all know that, when searching, \n is newline and \r is carriage return (^M), but when replacing \r is newline while \n is a null byte (^@). What is the origin of this asymmetry? Given that this ...
1
vote
4answers
132 views

Replace using info from the replaced string

What I want to do is replace this: <|Filename.ext|5|0|> with <|Filename.ext|5|0|> <|Filename.ext|5|1|> <|Filename.ext|5|2|> <|Filename.ext|5|3|> <|Filename.ext|5|4|&...
2
votes
1answer
194 views

Substitution using groups from regex works only on one group out of three

I want to replace if (condition) //comment \n\s*{ (\n - newline, \s* 0 or more whitespaces), or rather restructure it, so brace is on same line as if and comment is moved to next one and is indented ...
9
votes
1answer
1k views

What is '\%' and '\@' token's name in vim regex? And what does it do?

While editing syntax file, I found something like: \s*\%(\%(:\@<!\/\/.*\)\=\|\%(\/\*.*\*\/\s*\)*\)$ I'm familiar with any other than \% and \@ there. I can't even googling because I don't know ...