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
9
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
279 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
113 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
533 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
81 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
251 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
4k 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
528 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
578 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
80 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
122 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
169 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
101 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
160 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
115 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 ...
4
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
509 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
157 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
439 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
822 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
7k 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
173 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
510 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
6k 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
3k 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
1k 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
4k 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
169 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
84 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
497 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 ...
3
votes
1answer
416 views

Copying just the matches to a register?

I'm trying to copy just the matches in my regex search to a register in VIM on windows. That said, I found the following script for doing this: function! CopyMatches(reg) let hits = [] %s//\=len(...
7
votes
1answer
1k views

XOR two lines to see differences

I need to compute a "XOR"-like of two lines to find every char that differs between the two lines. I know the vim-diff feature can easily achieve that, but for comparing two lines I wanted to keep ...
2
votes
2answers
140 views

How to extract part of the current working directory to the statusline?

I have a lot of projects of the form: /home/karl/source/project-name/further/hierarchy/... I would like to extract just the project-name to put into my statusline. I use autochdir, so I know I can ...
9
votes
2answers
636 views

Duplicate lines beginning with “x”, replacing “x” in the duplicate with “y”?

I have a file with the following pattern: foo 111 baz foo 222 baz foo 333 baz which needs to be turned into foo 111 bar 111 baz foo 222 bar 222 baz foo 333 bar 333 baz So, duplicate all the lines ...
2
votes
1answer
6k views

Creating a regex to match html attributes

I know that there are a lot of answers to vim/regex, but I can't seem to find one that fits the bill for me. So, I'm trying to remove some attributes from HTML using the following: %s/( id="\| ...
3
votes
2answers
336 views

How to restore cursor position after search and replace in autocmd

I'd like to delete all spaces on empty lines before saving files. I've figured out how to do that. autocmd BufWritePre * silent! %s/\v^\s+$//e But this moves my cursor the line where the spaces have ...
2
votes
2answers
605 views

How to replace a fixed number of chars from the beginning?

I tried replacing 30 chars from the beginning of each line of my text, but could not figure out how to specify to vi sed to do it. I tried this: :%s/.{3}/xx/ But it could not find my pattern. I ...
1
vote
1answer
59 views

Embracing and debracing Greek passages

I need two commands: one to find all occurences of \gr{...} and remove the \gr{ and }, leaving the contents ..., which should work accross lines; and another to find all strings consisting of Greek ...
8
votes
1answer
1k views

How do I replace a character with its hex value?

I want to replace a character with its hex literal. For example before regex: h After regex: 0x68 I'm trying this: s/\([a-z]\)/\=printf("%#02x", submatch(0))/ But this replaces it with 00 ...
2
votes
1answer
212 views

Why double negative regex matches when positive one doesn't?

I have this regex \%(\w\|[^[:punct:][:space:]]\|\%(\\\\\)*\\\)\%#\|\%#\%(\w\|[^[:space:][:punct:]]\) What I wanted to is where this regex matches word characters \w match word characters except u ...
-1
votes
2answers
146 views

create list of strings more easily

Every time I need to create a list of strings in python or another language with similar list syntax (and they more or less the same everywhere) I have to do the following. # resulting expression a = ...
1
vote
1answer
70 views

Can I restrict :s to a particular substring of a line?

I ran into this doing a quick conversion of markdown into JIRA markdown. - A standard markdown list - hierarchical - easy to read - tidy - A JIRA markdown list -- Hierarchical -- Somewhat ...
4
votes
1answer
1k views

Is there a function which can return a list of matching text for groups in a regex?

Bash can natively match regular expressions in the [[ ]] construct, using the =~ operator. When it matches something, the result is stored in the BASH_REMATCH array variable. BASH_REMATCH contains, in ...
2
votes
2answers
64 views

How do I substitute with a character from the matched text, where the pattern contains a range?

If I'm using a range in a regex, how can I replace with the character that was matched? For example, lets say I want to find all occurrences of a or b, and replace them with aa or bb. Can I do this in ...
6
votes
4answers
595 views

Store the number of matches in VimScript function?

Is there any way to store the number of matches in a variable in a VIMScript function? For instance I'm using: %s/,//gn