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

Operate on third column of data

Is there a way to operate on a specific column of data without specifying the exact column number but only the column number? For example, if I have data like this: abc 123 45.6 jkq df 456 7....
1
vote
2answers
57 views

Avoid so much escaping in substitution expression

I have a substitute expression to find words beginning with a dollar signs and capture them like this: :%s/\$\(.*\)\>//g However there are a lot of escape sequences in the regular expression (4). ...
3
votes
1answer
459 views

How to put space before and after operators for enhanced readability?

I want - variable++; then variable ++; variable1 + variable2 nothing to do variable1 +variable2 then variable1 + variable2 variable1+ variable2 then variable1 + variable2 variable1+variable2 then ...
1
vote
1answer
229 views

What '\v' and '@<=' in '/\v(A)@<=b+\1' regex means?

The expression: /\v(A)@<=b+\1 What \v and @<= means? It's sure this is able to find 'bbbA' in "AbbbA'-s. It's trivial that the number n of b-s can be any n > 1.
0
votes
1answer
35 views

Matching delimited text, taking into account level

I am looking for a way to replace \cmd1{stuff \cmd2{other stuff}} with (stuff \cmd2{other stuff}) I have tried :%s/\\cmd1{\(.\{-}\)}/(\1)/gc However, this returns (stuff \cmd2{other stuff)} ...
1
vote
1answer
90 views

How to use zero matches in regular expression?

I have following text _printer _printer _printer _printer _printer _printer _printer _printer printer and I want to find the last one by using a search for the text printer which is not preceded by ...
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/...
0
votes
1answer
83 views

Highlight lines above cursor

I was looking for a way to highlight lines above the cursor line to mark them as done, whatever that means in the context of what I'm doing. I came up with this match Comment /\_.*\%#.*/ or, in words,...
0
votes
1answer
48 views

Newline in sub-replace expression

I've been trying to write a regex that turns the following structure # Ignore this line Foo Foo # Ignore this line too into: # Ignore this line IF BAR THEN Foo Foo # Ignore this line ...
1
vote
1answer
194 views

How can I use Tabularize to align by spaces, but ignore spaces in end-of-line comments?

Simple question: I might have content formatted like item1 item2 # this is a comment longer_name1 foobarbaz # another comment that I want to see aligned as follows: item1 item2 # this is ...
2
votes
2answers
119 views

What is the relationship between command line Ex and Vim?

I have read in various documentation that when Vim is started in Ex mode, it attempts to "act like" Ex, so I gather from this that Ex is a separate program and is not an actual part of the Vim source ...
0
votes
1answer
129 views

How can I match the shortest string including new lines?

In search expressions I know I can find the shortest match on line with the following expression: begin.\{-}end So for example, if the text is like this: begin end1 end2 end3 Then it will match up ...
0
votes
2answers
507 views

How can I visually select a regex match?

Sometimes I just want to see whether a regex is matching the right stuff. If I could cause Vim to visually select the matching text, it would be one way to do this. Is there a method for doing this?
5
votes
1answer
238 views

Can vim perform all substitutions simultaneously?

The root of my question is about a difference between the way Vim processes multiple substitutions and they way other editors (Sublime, for example) do it. I have this file: thing1 thing2 thing3 ...
2
votes
1answer
313 views

Sub replace if whole line match pattern

How to replace only if whole line match pattern? Example, input: foo, bar, baz # lorem, ipsum, ret Example, output: foo, bar, baz # lorem, ipsum, ret Should only replace commas with comma+...
3
votes
1answer
54 views

Explain the ./,-/\n$ pattern when using :global

This is probably a really simple question but I can't seem to figure it out myself. I'm using a command from this answer on Stackoverflow to "unwrap" text. The person who answered gave the command: :...
6
votes
3answers
816 views

Delete block of code if a pattern is (not) detected in one of the lines

I have managed to do this with a super long find-replace but I'm sure there's a simpler way: I have a file of the form: E01 2017 10 24 00 00 00-1.90001e-04-8.79280e-12 0.00000e+00 3....
0
votes
1answer
289 views

Search and replace multiple files [duplicate]

I have several files where I want to change a word in several files from oldWord to newWord. Please note I want to do this with the new cpo feature. I also would like to do this in one command line ...
1
vote
1answer
149 views

Character class to match “iskeyword” characters

In short: how do I define a regex that matches 'iskeyword'. My initial thought was that match(str, '\w*') would match an arbitrary number of keyword characters in str, but it really only matches to a ...
1
vote
1answer
337 views

Search query to match multiple instances on a line, only between certain delimiters?

I do a lot of programming in Ruby, and a common edit suggested by Rubocop (a linter) is to reformat an array of symbols [:foo, :bar, :baz] as a percent string: %i[foo bar baz] I'm looking for a ...
2
votes
1answer
36 views

Can I change the command mode (: …) so that I can have vi shortcuts?

Basically my question is this. On my terminal I have vi mode enable. It saves me lots of time editing command line arguments and zooming throught the file system. I would like to have a similar mode ...
2
votes
1answer
488 views

In Vim regex, Is there an abbreviation for the pattern dot followed by plus sign?

Given that the pattern .\+ is frequently used, is there an abbreviated way of specifying it in Vim Regex?
2
votes
1answer
105 views

How to anchor matchlist after searchpos?

I'm trying to find words before the cursor using searchpos, then use matchlist to get the captures. I can't find a reliable way to anchor the pattern for matchlist. For example, if the cursor is ...
0
votes
1answer
34 views

Do syntax patterns let you use very magic or not?

Quoting syn-pattern: See |pattern| for the explanation of what a pattern is. Syntax patterns are always interpreted like the 'magic' option is set, no matter what the actual value of 'magic' ...
1
vote
2answers
44 views

unable to replace text starting from *

I had following code:- Time::Time(short y,short m,short d,short h,short mi,short s): (*this).y(y), (*this).m(m), (*this).d(d), (*this).h(...
1
vote
1answer
362 views

Ignore pattern inside of a region

:h region bring me very little help. I need to exclude a pattern inside a region that should be ignored for example: don't apply match/highlight begin from # to the end of line $ I try to ignore #.*$ ...
0
votes
1answer
576 views

How to remove lines where the first “word” is non-unique

I have a large data file of comma separated variables. I need to remove all lines where the first entry (a number up to four digits) is non-unique. A typical example of what I am working with is below ...
0
votes
1answer
108 views

Substitute only on certain lines

Is there a way to have s;/;|;g run only on the lines I filter using a regex? I have a file, which looks like: Date: 2017/08/18 Path /home/share/thing Path /home/me/thing Date: 2017/08/18 Path /home/...
15
votes
0answers
360 views

How does `\zs` interact with syntax matches?

Consider the text: foobarbaz First we highlight the regular expression /foobar/: :syntax match Special /foobar/ Next we highlight the regular expression /foobarbaz/: :syntax match Error /...
0
votes
1answer
352 views

Find two consecutive non-empty lines

I want to find two consecutive non-empty lines. AAA BBB AAA BBB AAA BBB AAA BBB The file above shows four pairs of consecutive non-empty lines. All of the lines can have trailing ...
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 &...
0
votes
2answers
808 views

replace lines with the first and the last word

Here is a text with lines containing simple words: AZREG AREDB Z ARGHREHV AZRZEGV RYUJUS How to replace this line with only the first and the last word separated by a space? I have ended up with :! ...
1
vote
2answers
349 views

Highlight exported Go functions

Golang uses naming convention to separate functions visibility: exported (public) starts with a capital letter, non-exported (private) with a lower letter. I would like to highlight exported functions,...
2
votes
2answers
2k views

vim how to move last word of each line to beginning of line using regex and backreferences?

I've been trying to select all characters except the last word (2 groups where 1 group is everything but the last word and the other group is only the last word) :%s/^.\+\([^\w\+]$\)/\2\1/g is what ...
1
vote
1answer
78 views

How to remove all words which doesn't match the pattern?

I've the following content: a1a foo bar buzz foo b2b bar buzz foo bar c3c buzz foo bar buzz d4d where I can match all words consisting only alpha characters by the following pattern: /\<[a-zA-Z]*...
1
vote
1answer
449 views

Strange behaviour in Regex, double escape backslash

I've tried to debug an indent function i've written, and came over this strange behaviour. Here I try to detect some LateX tags (debug output): >echo previous \label{ch10.1} >echo previous =~ "\...
1
vote
0answers
34 views

How to access syntax elements

I try to inline one syntax in another syntax (to be explicit: CSS in JS template strings). I'd like to build on top of the existing CSS definitions and not create new ones. Question: Is it possible ...
0
votes
1answer
283 views

How to use the JS regex engine instead of the built in engine?

Is it possible to use the javascript regex engine instead of built-in one for search and substitute?
0
votes
1answer
365 views

arbitrary length regex?

a weird question that i can't seem to solve i want to do a search and replace, anything that says %d, %3d, %13d or anything like that i want to replace it with XXX I'm not sure how to deal with ...
3
votes
2answers
13k views

Regex to match any character including newline

I frequently find myself trying to do transformations like author = {{foo bar}}, to author = {foo bar}, and I can't find a regex to match the part in ...
3
votes
1answer
155 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
79 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
317 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
117 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
567 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
142 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
394 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 ...