Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [regular-expression]

Regular expressions (often shortened to "regex") are expressions written in a declarative language used for matching patterns within strings.

2
votes
1answer
21 views

Pattern to match the label of a JavaScript object field when the colon is not followed by another colon or an equal sign

I wrote a syntax highlighting .vim file. One of the expressions is used to color the label in a JavaScript object declaration: name: 'value' ^^^^ I want to see the name in a different color. My ...
0
votes
1answer
34 views

regex find and replace all with capture groups throws E488: Trailing characters

My text file looks like this Lastname, Firstname Doe, John I'm trying to get it to end like this using find replace regex Firstname Lastname John Doe This regex throws the error, although using ...
-2
votes
1answer
52 views

Deleting 3 lines of code every 4 [closed]

I want to sort my database by regex, but don't know exactly how to do it. The editor I use is Atom It's example of input (the entire file has 10,000 lines) x1.83 30.12.2018, 12:05:29 1386834 30 x2....
2
votes
0answers
30 views

Substitute regex group in prompt when replace with confirmation

I am using vim to replace with simple regex: %s/Foo\(.\)/Bar\1Biz/gc I wanna to confirm each occurence and Vim asks: replace with Bar\1Biz (y/n/a/q/l/^E/^Y)? Is it possible to substitute group (\1) ...
0
votes
0answers
25 views

Syntax RegEx optimisations

Below is a pretty massive regex to match all supported IANA schemes enclosed in angled brackets (e.g. <mailto:...>). It works as expected. However, on big documents, I feel the lag whenever I ...
1
vote
0answers
20 views

search and replace: remember case in substitute

I have to change strings: fetchrandomData to fetchReportingData FETCH_RANDOM_DATA to FETCH_REPORTING_DATA I can change ca_z and CA_Z to ca_t and CA_T respectively with command: %s/\v([Cc])([Az])(_)?...
0
votes
1answer
60 views

matchstr() acts differently when used in script?

I have these lines in a script: " go to start of file and search until the end call cursor([1,1]) let moreLinesToGo = v:true let b:qtags = [] while (moreLinesToGo) " search for lines like ":344f:",...
0
votes
0answers
21 views

Match everything except some characters and some strings?

I want to non-greedily match a string that don't contain ,, :, 。and #c, and I have tried this: ((,|。|,|:|#c)@!){-} which doesn't work. How can I make it work?
0
votes
0answers
23 views

Search multi-line records separated by multi-char sequence

I have been looking online for an answer for weeks now, but I am still at a loss... (I didn't learn much from syntax highlighting, and can't find any way to make \._ exclude a match) I have a file ...
0
votes
1answer
54 views

Matching 'Capitalized' words in Vim

Hello fellow mouse haters. I'm trying to use the following regex to match 'Capitalized' words: "\l\@<!\<\u\w\+" Which matches 'Main' and 'Canvas' in: public class Main extends Canvas Or ...
0
votes
1answer
17 views

How to match and replace the pattern [35+1] in vim version 7.4.160?

I have the file with below content: char abc[35+1]; char abc[35+1]; [123]; char a_wo_011[35+1]; I want to replace the [35+1] with say ABC But its not working. I am able to search the pattern with ...
1
vote
0answers
36 views

How to specify Perl encoding in Vim?

For a buffer containing text "กขค" with UTF-8 encoding: :perldo s/ข/x/g => "กxค" but :perldo s/[ข]/x/g => "xx�xxxxx�". How to specify Perl's encoding? Note: Currently I have to use %:!perl -C -...
0
votes
1answer
23 views

regex characters replace multiple characters

what is the most efficient regex (vim/perl) to replace 10 'a' characters with 1 'c' character followed by 100 'b' characters? this does not work: :%s/a{10}/\=repeat('c',1)\=repeat('b',100)/g
0
votes
1answer
30 views

regex vim multiple characters in search, multiple characters in replace

what is the most efficient regex (vim/perl) to replace 100 'a' characters with 300 'b' characters. I can get the search part correct :%s/a\{100}/?????? but the 'replace' portion eludes me.
1
vote
2answers
22 views

Swap grid[X][Y] to grid[Y][X] in full file

In my file, I have many instances of grid[X][Y] where X and Y are variables. I would like to swap all instances to grid[Y][X] (preferably one by one so I can check them). Examples (especially note ...
2
votes
1answer
34 views

Search and replace: float arithmetic

For example I have many numbers like 111.222, I want to decrease them to 0.1 (to get 111.122), how to do that? I tried: :%s#\d\+\.\d\+#\=submatch(0)-0.1# But as a result I have 110.9.
1
vote
2answers
30 views

Multiline regular expression excluding characters

I am designing a syntax rule (for R code) that highlights if I do not start a function with a curly all by itself. For example, A <- function(x, y){ would be match but I do ...
0
votes
1answer
25 views

How to use a regexp to search/match specific pattern only for the custom statusline and/or tabline?

I got this regexp from google which is for matching specific glyphs in vim-nerdtree. autocmd FileType nerdtree syntax match {hl-group} /\v{glyph}/ containedin=ALL But obviously it won't work on ...
0
votes
1answer
48 views

how to check if the value of a variable has a specific character?

First of all sorry for the dumb question but I already searched google but did'nt find anything (or atleast did'nt understand it at all).. I'm trying to make a vimscript that will read the ....
0
votes
1answer
41 views

How does the following regex works

I just want to learn how vim regexp works and to be able to compose my own regexp for example, I have found this on google which searches for matching .sh files #^\s\+.*sh$# How does this work?
3
votes
1answer
54 views

replace or operate within regex match

underlying question: Is there a way to operate on text within a regex match? I.e. is there an analog to the 'g' command that operates on match substrings instead of lines usecases: One example of ...
0
votes
2answers
86 views

Replace every second space on each line?

I want to find the most elegant way to replace every second space on each line with Vim. Most elegant solution accepted. Example input 123 1234 1345 123456 12344567 12345678 123 1234 1345 123456 ...
1
vote
1answer
33 views

Adding a quantifier to group

I have the following file (I used 4 spaces instead of tabs here): abc123 abc123 abc123 abc123 abc123 abc123 abc123 kaskls abc123 kaskls The alphanumeric entries are separated ...
0
votes
3answers
48 views

Append `0,` to all lines having 2 commas and not more than 2 commas?

I have a file 0,3, 7,2,0, 1,2,3, 1,2, which should be 0,3,0, 7,2,0, 1,2,3, 1,2,0, So how can I append each line with two commas with 0,? I am interested in the most elegant solution and ...
0
votes
1answer
37 views

Transform lines: prepend with incrementing number and string and append with string for each line in one step

I have the following lines: abc07 bca08 xyz17 And I would like to transform it to: [0]="abc07" \ [1]="bca08" \ [2]="xyz17" \ Based on this question, I can do the first part and the end ...
1
vote
1answer
66 views

How to visually select a pattern only on current line using vimscript?

I'm trying to write a vimscript function that will allow me to map a key to select a regex match on the line my cursor is on. This will help with selecting strings for use with ctags. For example, I ...
4
votes
1answer
100 views

How to search literally without any regex pattern?

Does vim allow for searching raw strings? If what I want to search is in the variable string, does the following code work? call search('\V' . escape(string, '\')) Or are there other more direct ...
0
votes
1answer
29 views

Precision format in Vi

Is there a way to format numbers as per a precision format in Vi? I have a log file with a huge list of hexadecimal numbers, I would like to remove the '0x' prefix; then make the numbers show up ...
2
votes
1answer
36 views

Batch change name from ABC - DEF.pdf to DEF - ABC.pdf

I want to batch change 50+ files in a folder in Windows10. the original name style: 'how to install the software - softwareA user manual p1.pdf', the new file name will be 'softwareA user manual p1 - ...
3
votes
1answer
50 views

How can I get Tabular to only work with certain lines and to use spaces as a delimiter?

How can I get Tabular.vim to only consider and affect lines that match /^item\s/, to use /\s\+/ as the delimiter, to left-align everything, and to use at least three spaces between words? For example:...
1
vote
1answer
62 views

Highlight whitespace using matchadd

I'm trying to use matchadd to highlight different occurrences of whitespace. However, sometimes it doesn't work for some reason. Here is the code: highlight GroupA guibg=lightgreen highlight ...
1
vote
1answer
112 views

Copy first letter of each word in a line

I would like to copy the first letters of paper titles in a bib file and set that as entry name. Example: @article{Cqmdoprbcc, title={Can quantum-mechanical description of physical reality be ...
1
vote
1answer
38 views

Match and replace very specific number

I would like to replace every number n in my document with n+1. Easy: :%s/\([0-9]\+\)/\=(submatch(0)+1)/g Now I would like to replace every number n with n+1, only when the number has exactly three ...
0
votes
1answer
60 views

Non-greedy regex quantifier is greedy

Note: I am not looking for a working substitution for my command (allready have that), but for an explanation. I have the following code: include <stdio.h> char ga[] = "abcdefghijklm"; void ...
0
votes
0answers
49 views

Highlighting a search capture group

By :set hl, the search result of a / expression is highlighted, for example: And if one wants to replace one substring distinguished by its surrounding, one can do so elegantly using regex capture ...
0
votes
1answer
70 views

Is it possible to return to normal mode in the middle of a vim command?

By simply calling :(range)g/bob/norm Axxx I can add xxx to the to the end of all lines in range containing bob. Is there a way to return to normal mode after having used A/I/a/i to enter insert mode, ...
1
vote
3answers
77 views

Add space after parens

My Problem I am editing a markdown file with some links in a [link text](url) format. Some of these links lack a space after the closing parens, e.g.: ...fit the [Single Responsibility Principle](...
0
votes
0answers
19 views

run previous regex replacement on a vim block

An antipattern that I do is this: Go to a line :s/foo/bar/g Go to the next occurrence I want to fix select a block of lines use & to rerun the replacement see that doesn't work manually count the ...
3
votes
4answers
105 views

How to run a python command based on a matched group of a pattern?

Let say I want to run a command that uses a matched group of a pattern for each line matching the pattern. The natural choice is :g. With the command :s, there is no problem. If i would do :g/aaa\(....
1
vote
2answers
88 views

How to match braces or brackets?

Suppose I want to search for things like \todo{English sentence} Then the regex \\todo{\_.\{-}} works (I don't know why. I copied it from somewhere long time ago (embarrassed)). But suppose I ...
1
vote
1answer
68 views

vim word boundaries in syntax files

I have a syntax file with the following definitions: syn keyword pythonBuiltinFunc __import__ abs all any \ bin bool bytearray ...
1
vote
0answers
18 views

Syntax - Match word based on a previous match

I want to highlight all words that have been matched in another syntax region. Using something similar to regex capture groups, for example given a file: {highlightme} asda asasd asd asd as ...
1
vote
1answer
25 views

Duplicating a row and modifying the duplicate as a macro or regex

Given a file with multiple lines matching this pattern (apologies for the length!) $this->execSQL('INSERT INTO `table_lowercase_and_underscores` (`id`, `field1`, `field2`, `field3`, `field4`, `...
1
vote
2answers
108 views

How to redirect search results into a new file?

Let's say, I want to search for email addresses and output them to a new file. What I can do so far with a help from another discussion is this: g/pattern/.w! >> output.txt but that saves ...
0
votes
1answer
667 views

Replace matching parentheses

Is there a way to match matching parentheses in vim regex? For example, if I have a line like this: function("test(with)parentheses"); // (comment) Command :.s/\(function\)(\(.*\))/\1[\2]/g ...
10
votes
3answers
611 views

What's the difference between the '\zs' and '\@<=' atoms in Vim regex?

This is what I get from the documentation: \zs "starts the highlighted part" after matching the preceding regex, and \@<= "starts the highlighted part" after matching the preceding atom. But I don'...
1
vote
1answer
32 views

Color Stuff Inside Braces in a Desired Way With a Trigger

Suppose I have \frac{stuff}{matter} somewhere in a file I am editing. And I want to change stuff and matter. So at the trigger of a command, I'd like to see the following take place: stuff gets ...
1
vote
2answers
558 views

How to match for diacritic combinations

I am editing a tex file using vim, searching for words starting with capital letters, as they are most likely proper names or names of works to which I'd like to add some formatting and/or indexing ...
0
votes
0answers
49 views

formatlistpat — unexpected regex behaviour

Here is a corrected version. The formatlistpat now works. The regex behaves as expected. I leave this question open, because during my tests I came across a warning saying I had too many brackets (I ...
0
votes
1answer
51 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 ...