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
15 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
22 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
23 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
26 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
40 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
48 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
54 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
24 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
44 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
32 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
37 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
93 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
33 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
42 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
50 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
109 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
37 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
53 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
33 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
66 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
60 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
87 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
76 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
53 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
91 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
436 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
558 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
429 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
45 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
47 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
284 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
64 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
0answers
25 views

Condition in substitution does not work

I have the following lines: \diagentry{M_0}\\ &\diagentry{M_1}\\ &&\diagentry{\xddots}\\ &&&\diagentry{M_{L-2}}\\ &&&&\diagentry{M_{L-1}}\\ I need to do a ...
0
votes
1answer
65 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
81 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
38 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
100 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
56 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
49 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
77 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
75 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
69 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....
0
votes
2answers
40 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
237 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
94 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.