Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

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
53 views

Setting the search pattern to just a portion of a regex pattern

I'm not sure if the title makes sense, but I'll try to explain what I'm trying to do. I have a file with some code like: some_var = 3; ... func(some_var); The file has a bunch of calls to func and ...
1
vote
1answer
613 views

Find the nth word in a line, and replace it back into the line in different position

I have searched unsuccessfully everywhere for an answer to this question and could not find an acceptable answer. I am taking a list of table names from a database, and dynamically creating alter ...
3
votes
2answers
99 views

How to do a find with a regex and jump to a specific position within the match

For example: I have several lines starting with :q:, which I want to jump to: :q: regex syntax (...) :q: regex examples (...) Now I want to jump to the start of regex syntax or regex example with a ...
0
votes
1answer
327 views

Why does vim not highlighting leading and trailing spaces? [duplicate]

I want trailing and leading whitespaces to be highlighted by vim. I created a ~/.vimrc file and appended this: " highlight trailing and leading whitespaces highlight TrailingWhitespaces ctermbg=red ...
3
votes
1answer
98 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
0answers
59 views

Disable regex searching in specific windows

I usually have about 4 or 5 windows open in Vim. Currently - one of my files is very large(in size) - a CPU cache trace. Every time I search with "/", vim searches all windows before returning the ...
0
votes
1answer
736 views

How to format csv with regex

I have a csv that looks like this: 1,wer123,name,address1,age,2,cde789,name,address1,address2,age,3,529owq,name,address1,age and so on. I want this csv to be formatted as 1, wer123, name, ...
0
votes
1answer
109 views

loading back-reference groups “shortcut”

I'm looking for a shortcut for loading back-reference groups using the same separator. Such a beast may not exist but it would be useful to me if it does. An example: Starting with this string :111 ...
2
votes
1answer
66 views

Break-down long strings and search/replace individual words

I am trying to do a search and replace in vim (rather, gvim) and keep getting stuck up trying to separate words. I need to convert something like this: (COND <ASEL&~ASELb|XOR|~XORb> ( To ...
0
votes
2answers
116 views

C - Generate .h file from .c file

Is there a good macro or regex expression that I can use in Vim to generate a .h file from a .c file? For example, take the following .c file // temp.c // comment 1 // comment 2 void function1(int a,...
2
votes
2answers
88 views

How to replace string with value and the occurance number?

I have a beamer(LaTeX) file containing many \begin{frame} lines. I want to debug the code to viewing the output of its. The pdf output shows frame number. When I try to change a specific numbered ...
0
votes
2answers
143 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
60 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
531 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
270 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
103 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
91 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
53 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
225 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
124 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
610 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
257 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
354 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
91 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
844 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
328 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
158 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
411 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
564 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
113 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
45 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
392 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
587 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
116 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
402 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
395 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
418 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
890 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
379 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
3k 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
95 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
519 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
322 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
395 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 ...