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
1answer
917 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
79 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
42 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
182 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
117 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
125 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
433 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
224 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
280 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
48 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
793 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
259 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
145 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
259 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
437 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
101 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
33 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
340 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
573 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
95 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
318 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
312 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
285 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
759 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
316 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
68 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
419 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
262 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
315 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
12k 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 ...
2
votes
1answer
147 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
78 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
284 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
111 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
527 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
41 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
134 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
373 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
140 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
37 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
310 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
148 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
523 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....