We’re rewarding the question askers & reputations are being recalculated! Read more.

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
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
187 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
180 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
538 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
58 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
74 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
141 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
273 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
734 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
6k 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
70 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
133 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
207 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 ...
10
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 ...
2
votes
1answer
299 views

Understanding the beginning and end of word pattern-atoms

In the Practical Vim book, there is an example of a regular expression that matches duplicate words: /\v<(\w+)\_s+\1> While this works beautifully, I don't quite understand why it also matches ...
4
votes
1answer
2k views

Escape quotes within quotes in command

I'm trying to run a command (from within a function) that goes somewhat like this: execute a:firstline . ',' . a:last line . 'g/\(\.\|!\|?\)\()\|]\|"\|'\)*\($\|\s\)/execute "normal! v(:call ...
2
votes
2answers
125 views

Replace last instance of character (and there's more)

I am trying to format a list that uses tabs for indentation so it will render as a bulleted list on Confluence. That means that each instance of a tab needs to be replaced with an asterisk (*), plus ...
4
votes
3answers
578 views

Run a function once on every paragraph

I'm trying to write a plugin that highlights overused words. After much effort I managed to write a function (and a command, :Ditto) that will highlight the most used word in the file or in a ...
4
votes
1answer
7k views

ignoring spaces in search like ignoring cases

I frequently search in my files using /, and \c is really helpful in these situations. Is there any similar way to ignore white space and line breaks? It would really be nice to be able to add ...
1
vote
1answer
84 views

Find a non-regular expression tag in a specific tags file

I have a tags plugin that I'm adding features to. In order to test that it is working correctly, I need to examine the tags inside a tags file. I am trying to make sure the correct tags appear in the ...
0
votes
0answers
262 views

Matchit Plugin: issues when resetting regular expressions in “b:match_words” for detecting Matlab file keywords

I'm trying to implement a custom regexp string to be used by matchit.vim for identifying matching keywords that can be jumped between with %. I found a utility from FileExchange (in the ftplugin ...
8
votes
10answers
5k views

Substitute second occurence on line

I have a list of files: ./a.temp.txt ./a.temp.txt ./a/b.temp.txt ./a/b.temp.txt ./a/b/c.temp.txt ./a/b/c.temp.txt And I want to remove the temp. on each line, but only the second occurence, ...
0
votes
1answer
593 views

Yank all function names in a file

How would I yank all the function names in a file? For example in C, you might want to take all your function definitions in foo.c, and grab them for declarations in your header file foo.h. void ...
5
votes
2answers
608 views

Vim conceal indentation. Replace indentation spaces with utf-8 chars

I use four spaces for indent. I would like to replace the indentation with some UTF-8 chars. What I have: 1. def foo(bar): for jaffa in bar: print jaffa 2. var foo = function(bar){ ...
1
vote
1answer
81 views

Split a line by non-letter characters

Let say I have a minified javascript function like this: function(){for(a=0;++a>0;){console.log(a);}} and I want to apply an agressive split on it, that is insert a new line for some characters. ...
4
votes
1answer
124 views

Your tips to learn regexp [closed]

I see a lot of members here manipulating and using regexp very easily. I am slowly learning, one trick at a time, how to search for this or that, how to handle this case, how to escape that case... ...
3
votes
2answers
83 views

How to move multiple IPs to different lines

I have a program that lists hostnames and IPs. Some hostnames have multiple IPs assigned to them. When I download the IP column .csv, it lists as such: 192.168.xx.xx "192.168.xx.xx, 192.168.xx.xx" ...
3
votes
2answers
170 views

Search for mismatched length alternative pattern?

My file consists of the following text one bone stone cuttlebone minestrone cornerstone and I want to select all instances of bone and stone, so I get the following highlighted text: one BONE STONE ...
3
votes
1answer
105 views

'Very nomagic' doesn't work as expected

:h \V tells me: Use of "\V" means that in the pattern after it only the backslash and the terminating character (/ or ?) has a special meaning. "very nomagic" However: :echo ')' =~# '\V\[)]}&...
5
votes
1answer
175 views

In a regex, why aren't the multi + and {1,} always parsed exactly the same way?

From :h /\+: \+ Matches 1 or more of the preceding atom, as many as possible. And :h /\{: \{n,} Matches at least n of the preceding atom, as many as possible It seems the multi + and {1,} have ...
1
vote
1answer
121 views

Braces don't match with regex equals operator

function! s:foo() normal! yiw if @" =~# '\vf[0-9]b\(' echom 'Matches!' endif endfunction nnoremap <silent> * :<c-u>call <sid>foo()<cr> " f2b⣿( When my ...
5
votes
4answers
2k views

Add constant integer to visually selected numbers

I have a file like the following : two different files were yanked one after another, and the numbers subsequently don't match anymore. ... 46 atom 142 option 47 atom 143 ...
5
votes
1answer
563 views

How do I ignore regex failure while running a macro?

If I have a regex in a recorded macro and then run it over a line that the regex fails to match the macro stops. For example, I have a list of files. family-reunion-2015.webm road-festival-20150607....
4
votes
1answer
164 views

Backward search command ignores “\” in regexp “o\?”

I'm using Vim 7.4. I tried to search backward for front and frnt, so I pressed ? and entered the regular expression fro\?nt. Vim tells me that it can't find fro?nt (the backslash is missing in the ...
15
votes
2answers
440 views

How can I run :substitute on only part of the line and not all of it?

I have a line like this one: function my_function(argument, another argument, but there are spaces) I want to substitute the space characters (inside the argument's names) with underscore. I thus ...
2
votes
1answer
852 views

Matching matching brackets

How can I match a pattern like \\gr{\_.*:} but so that the brackets have to match? That is, \gr{this should match:}, \gr{this should {not:} match} \gr{nor} should {this:}.
2
votes
5answers
8k views

Replacing last comma on each line

The following will replace the first comma on each line with a pipe: :%s/,/|/ How can I do the same for the last comma?
1
vote
1answer
174 views

How to remove lines matching pattern in all files changed in one editing session?

I'm trying to create function and a mapping to insert/remove breakpoints in python code. So far I got this function! AddBreakPoint() let l:line = line('.') let l:indentChar = ' ' call ...
3
votes
3answers
546 views

Reduce two consecutive character to just one and remove if that character is standalone

Converting odt to markdown produces \ around special characters. So I need to change \\ to \ and remove if \ is standalone. Sample text: * \[\] match any of the character specified within \[\], use \...
5
votes
2answers
7k views

Regex to match spaces that are NOT at the beginning of a line?

I want to remove repeated spaces, but not if they occur at the start of a line (indendation). Before: line 1 hi there i have spaces After: line 1 hi there i have spaces I've ...
3
votes
4answers
4k views

delete repeated line in vim, keep the n-th line in Vim

I want to ask how to delete repeated lines in vim. For now, I have a file which contained 12740 lines. There are only 98 unique lines, and each line is duplicated 129 times. I have tried: g/^\(.*\)$\...
4
votes
2answers
2k views

Searching/replacing exact word in vim by default

In vim when searching or replacing an exact word you need to type: \<word\> to match it exactly, this is cumbersome to type, and I find myself wanting to search/replace exact words a lot more ...
8
votes
2answers
5k views

Nested matching group in regex

I have a common usecase when I transform some python expression the following way: value 1 value 2 value 3 into ['value 1', 'value 2', 'value 3'] The easiest way may be to use a mapping, but I ...
5
votes
2answers
174 views

Efficient ruby line splitting

I never made any plugins but I would like to get some advice for a specific case. I'm writing a lot of ruby. Sometimes I have very long lines that I want to split. Example: MyModel.first_method....
0
votes
1answer
88 views

E890 Trailing char after ']' - vim/neovim regex

When I reload my syntax file I receive this error: E890: trailing char after ']': /v(.)@<=w+[nrs]/ Here's the line it's bugging out on, does anyone see why this doesn't work? syntax keyword ...
3
votes
2answers
68 views

How to search for a pattern that starts at newline in ex/vi

How do I search for pattern that is not at the beginning of a line? Ex: Name1 Name2 Name3 Name4 Name5 Name6 Something like: /[^^]Name. Obviously the second ^ symbol does not mean beginning of ...
2
votes
2answers
541 views

Extra trailing whitespace in regex match using “&”

In order to overcome my irritation with quoting and comma separating words in a list, I thought I would play with Vim's search and replace to allow myself to type lists like this: daqs = [this is a ...
7
votes
2answers
177 views

Executing a command on delimited lines

How can I run a command on all lines delimited, say by \begin{otherlanguage} and \end{otherlanguage}? For example, 1 This line should not be affected, 2 \begin{otherlanguage} 3 but ...