Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. 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
2
votes
1answer
28 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
87 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
55 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
32 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) ...
1
vote
0answers
30 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
113 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
123 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
1answer
109 views

Matching 'Capitalized' words in Vim

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 another example, matches all ...
0
votes
1answer
26 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
39 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
98 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
48 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
31 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 ...
3
votes
1answer
276 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
64 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
49 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 ...
1
vote
1answer
409 views

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

I'm trying to make a vimscript that will read the .gitignore file, return it's values as a List and check for paths that has wildcards eg: /vim/snippets/*, vim/**/*. let g:ignored = readfile("/home/...
0
votes
1answer
44 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
61 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
517 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
135 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
60 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
50 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
139 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 ...
6
votes
2answers
189 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
38 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
40 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
111 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
113 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
126 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
41 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
137 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
2answers
219 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
93 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
127 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](...
3
votes
4answers
253 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
541 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
152 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
24 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
28 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
290 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
2k 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 ...
11
votes
3answers
930 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
44 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
1k 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
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
602 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
95 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
307 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
97 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/...