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

Traditional Vi and its regular expressions

I'd just want to get confirmation on (and maybe some background to) the way that Bill Joy's vi, sometimes found as traditional-vi on some BSD systems, handles regular expressions. It seems as if most ...
1
vote
1answer
80 views

Search pattern for an if statement that isn't followed by curly brace on the next line

How would I formulate a search pattern for matching if statements that don't have an opening curly brace on the next line? I assume it requires the use of something like [^{] or \@!, but I'm having ...
2
votes
1answer
331 views

How do I remove newlines inside arbitrary xml tags matching a pattern?

I'm working with large xml files that have tags, and some are spread across multiple lines, and some are on a single line. I'd like them to all be on a single line. There are other tags in the files ...
0
votes
1answer
130 views

Copy a substring in line to the front of the line

I have a list of file names I'm trying to create insert statements out of. In each file name is the ID I need for the primary key, that I just need to copy from inside the file name to the front of ...
6
votes
1answer
608 views

What is an atom?

I have been looking through many resources but cannot seem to find the definition of an atom. In the website link http://vimregex.com/, how does an atom defer from a character in regex? \{-} matches ...
3
votes
1answer
43 views

How to check if a pattern is contained in a register's content?

I'm writing a new plugin and as part of a function, I need to check that a register contains a particular pattern. I checked the official documentation but couldn't manage to use any of the search() ...
5
votes
5answers
3k views

Match any number of digits NOT followed by

I want to search for: From start of the line, any number of white spaces followed by any number of digits NOT followed by a dot. And replace it with all that was searched and a dot. This is just ...
2
votes
2answers
149 views

Understanding “\_W*” in vim regex

I have been going through some answers to a vimgolf, and stumbled upon this regex: :s/\_W*/-/g|x<CR> Which converts abcdefghijklm to -a-b-c-d-e-f-g-h-i-j-k-l-m- The regex (without ...
1
vote
2answers
445 views

Replace two word sequence with second word

I need to replace on all lines sequence of words: public boolean public string public IList etc.. with modifier removed aka: boolean string IList Is there a way how to remove first part of match ...
1
vote
2answers
1k views

replacing urls and escaped urls

I am trying to do a search and replace on a large database file where some of the urls are escaped and some not, e.g.: http://www.example.com http:\\/\\/www.example.com I thought I could use a ...
2
votes
2answers
155 views

Substitute text on one line with capture group from previous line?

Suppose I've copied a bunch of lines of boilerplate code that I need to copy multiple times over, and then change in two separate places, in the same way: it 'includes sessions#new' do expect(...
0
votes
1answer
38 views

How to use regular expressions with \w\d inside ()

I created an example of text: dog cat worm And I want to replace everything but not the first word and without knowing what's there. At first I tried this::%s/\(dog\)\(.*\)/\1/g This works fine ...
0
votes
3answers
354 views

How to do the following replace in vim?

I want to replace all of the occurrences of armv7_format_strings<any_string> = {<str1>, <str2>}; by armv7_format_strings<any_string>[0] = <str1> ; ...
0
votes
1answer
173 views

How do I use :Tabular only for delimiters after a certain pattern?

How can I use the Tabular plugin to align by ";" after "//"? With text: Line 1 ; B // Comment 1 ; Comment 2 Line 2 C // Comment 3 ; Comment 4 After running :Tabularize /; I get: Line 1 ...
0
votes
1answer
28 views

Tabularizing all Commas after `EST[`? [duplicate]

How do tabularize all commas after instances of EST[ so that text like Item 1 //EST[5555m, 6666m] Item 2 //EST[1m, 300m] gets transformed into something like: Item 1 //EST[5555m , 6666m] Item 2 //...
2
votes
2answers
557 views

Regexp for beginning of word?

I am trying to search backwards (using ?) for the beginning of a "word". I don't mean a word in the text object sense; I mean a series of lower-case letters optionally beginning with a capital letter....
4
votes
1answer
213 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
93 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
73 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
342 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
461 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
468 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
5k 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
104 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
235 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
791 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
221 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
368 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
339 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
276 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
5k 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
79 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
203 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
166 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
137 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
447 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
1k 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
222 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
584 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
717 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
170 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
139 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
159 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
99 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
320 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
2k 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 ...