Stack Exchange Network

Stack Exchange network consists of 175 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.

1
vote
1answer
6 views

(Dynamical) quantifier in search and replace

In VIM, using search and replace like :%s/pat/repl/g, is it possible to have a quantifier in the replace part? As an example, replace every line matching pat by exactly 100 # without actually typing ...
0
votes
0answers
18 views

How to pass '\v\C[pattern]' into the built-in match() VimScript function

I am attempting custom fold logic as my first VimScript. What works for me: USING VERY MAGIC (\v) and explicit CASE SENSITIVE (\C), find python comments in foo.py that begin with hash, then Roman ...
2
votes
4answers
63 views

Search for line, move to end of search string, then search+replace

(EDIT: In the original question, I had accidentally used \w instead of \<. Comments and answers with \w should be interpreted as using \< as well.) In my LaTeX source files, section headings ...
1
vote
2answers
36 views

Search and replace only if following character is “x”

I'm trying to make a search and replace to format the following type of strings. INPUT someName0 : in std_logic; someName1 : in integer; someName2 : out unsigned; someName3 : out std_logic_vector(3 ...
2
votes
1answer
37 views

Using (neo)vim's regex to match up to but *excluding* a certain character?

I have a quick question about the regex (neo)vim uses. After long hours of toiling, staring at documentation and trying everything else I could think of, I seem to be stuck. What I'm trying to ...
1
vote
0answers
42 views

Basic and extended regular expression

I have a question: How to search a string using a basic regular expression in Vi editor? How about using extended regular expression in Vi editor?. Is it a real difference between basic regular ...
1
vote
1answer
70 views

How to match each occurrence per line?

I have similar text on more lines: _AB_ _ABC_ _ OR _ _A_ _ABC_ I need to replace only the underscores that delimit some text. So I need: =AB= =ABC= _ OR _ =A= =ABC= How to match each pattern?
0
votes
1answer
26 views

Using submatches inside and outside of system() call

I have this substitution, which does not work: exe 's/<<\(.*\)\%(,.*\)\?>>/<<\1,'.system('ark headings \1 | head -1').'>>' What I want to do is: capture some expression ...
3
votes
1answer
40 views

Vim RegEx question: how to detect headings of markdown(.md) i.e. lines composed of '-' or '='

I'm learning Vimscript in Learn Vimscript the Hard way and trying an exercise. Here is this: The lines "underlined" with = characters are treated as headings by Markdown. :onoremap ih :<c-u>...
0
votes
2answers
37 views

Vim: Foldtext contain all content, instead of only the first line

Would like to have all content of the folded block, instead of only the first line. My current foldsettings are: set foldmethod=marker set foldmarker={,} Example how my data is structured: { ...
0
votes
1answer
34 views

Looking to substitute roman numbers

I have a huge text file with Roman numbers for the paragraphs and I want to substitute them: text text ... text I text ... II and so on My question is: what regular expression describes one ...
0
votes
1answer
18 views

Why doesn't the + regular expression quantifier (one or more) work when searching?

I have a file with the following lines: a=117 a=100 a=101 When I try searching using /a=10*, it correctly matches all of those lines. But when I try searching using /a=10+, it does not match any of ...
0
votes
2answers
40 views

How can I edit multiple lines with a specific pattern?

I have a *.txt file with 10,000 lines. The lines are E-Mail and password divided by a ":", so like E-Mail:Password I want to delete the E-Mail addresses on every single line at once, so I end up ...
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
42 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
31 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
22 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
68 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
25 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
78 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
21 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
38 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
40 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
35 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
26 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
79 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
37 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
32 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
129 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
42 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
175 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
47 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
50 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
38 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
83 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
102 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
33 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
39 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
54 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
85 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
117 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
78 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
76 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
74 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
80 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
131 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
162 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 ...