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
0
votes
0answers
34 views

Regex match inside XML container

I'm trying to match data in a gnucash record, without skipping into the next record, so basically needing to stop matching at the closing tag. The record looks thus, for example: <trn:split> ...
0
votes
1answer
29 views

Yank both sides of equation

While writing in latex I often want to copy a math equation's right hand side or left hand side. For example I have something like this \begin{gather*} 5 \cdot \left( A A 3 \right) + 50 \cdot \...
0
votes
1answer
36 views

What alternative to capture groups should I use in VimScript?

I understand that Vim does not support regex capture groups. The problem I am trying to solve is being able to easily capture parts of lines. For example, given the following line of python source ...
1
vote
3answers
65 views

Search for line and paste as column until next match

I'm wondering if this is possible, more for curiosity's sake, but also it might be useful to know. I have several ascii data tables and I want to combine them all into a single file. So I combined ...
0
votes
0answers
36 views

can not make this regex pattern work in “very magic” mode

I am trying to construct a regex pattern to match characters inside two # characters. For example: #abc#bc#cd# I would like to match abc, bc and cd. The following regex without using magic mode ...
0
votes
2answers
49 views

Finding Markdown image tags with regular expressions (E486)

I have a document that contains lines like ![My title](/path/to/image.jpg) This is the format for images in Markdown. I have constructed a regular expression which I believe should be able to find ...
3
votes
1answer
46 views

How to replace the first matching for each line with regular expression

Saying that I have such a file as below: <random_string>hello<random_string> random_string<random_string>hello<random_string> I want to remove the first square bracket and ...
-1
votes
2answers
33 views

Increase numbers by one in a visual block

I edit DocBook XML files. In my plugin, i want to implement a function that would increase or decrease the section numbers recursively, assuming that cursor is at the toplevel section. For example, ...
0
votes
0answers
34 views

Regex to remove newline; works in one vim instance, but not another

Trying to remove newlines with a regular expression (not shift-j). I have one vim install where :g/conf/s/\n// works as intended, removing a newline from all lines containing 'conf'. I have a ...
0
votes
1answer
36 views

Why doesn't this `:execute` command work?

I was looking at this question on Unix & Linux Stack Exchange, and I thought I could write an answer for it using Vim. I ran the following for a quick test, and was surprised to find it didn't ...
6
votes
3answers
213 views

regular expression to find lines containing multiple specific words or patterns in any arbitrary order

Suppose we have the following very simple file: 1 x 3 x x 3 x 1 Now I'd like to have a pattern that matches all lines containing both 1 and 3. The order and position should not matter. (Note that I ...
1
vote
1answer
49 views

Substituting (*x).y with x->y

I am coding a C project and as I am unexperienced, everytime I needed to access the element of a pointer to a struct I typed (*x).y (where x is a struct and y an item), which I just realized could be ...
0
votes
2answers
26 views

Can capture groups be used like perl?

I'm trying to determine how many # characters are at the beginning of a line. In perl, I can so something like this: $match =~ m/^(#+)[^#])/; $length = length $1; Trying to do something similar in ...
1
vote
4answers
156 views

In Vim ex mode how to re-use a string from the default buffer in the command

I'm trying to modify this file, by extracting the very first line in the file and prepend it to each of the consecutive lines until end of the file black widow card Friday berry I used ex command (...
5
votes
3answers
175 views

How to get all distinct words within a set of lines?

I would like to extract a list of distinct words from a set of lines. Is there a way of doing this ? Say for example I have lines that look like this: [ [(isPhysicallySettledFxFwd, NO,"Y"),(...
0
votes
1answer
22 views

Why does backslash-escaping a CJK character cause a very-magic regex match to fail?

My understanding of very-magic regexes is that you can escape any character outside [A-Za-z0-9_] with a backslash to make it match literally. However, I don't see that behaviour with CJK characters. ...
2
votes
1answer
67 views

How can I search for Unicode characters by hex code in very magic regexes in vimscript?

I am working on UTF-8 support for my editorconfig-core vimscript plugin. I have hit a strange snag. This is in official GVim 7.4 Win32. I would like to get this working on 7.4, rather than ...
1
vote
0answers
38 views

custom fold expression to fold functions

I would like to create a custom fold expression for functions of the form x <- function(a,b,c) { ... ... body ... } where the lines starting from function(...) until the closing brace are ...
0
votes
1answer
19 views

Can you run a regex on a register or the clipboard contents?

I want to copy something to a register or more ideally the system clipboard and then run a search / replace regex on it the way that I do in the buffer...is that possible?
0
votes
1answer
29 views

Can you match unicode blocks by name in vim?

\p{Cyrillic} Works in my normal regular expressions. When trying to match in Vim or gvim however, I can not seem to get it to work. I tried something like this: \p\{Cyrillic} Is it not supported, ...
1
vote
1answer
41 views

How can I format a function declaration to force curly brace at the end of the line?

I have big portions of code where functions are formatted with the opening curly brace on the next line, like this : function foo() { } I'm trying to figure out how to search / replace in order to ...
2
votes
1answer
33 views

How to match file pattern, with extension, not containing a specific word?

I'm trying to use autocmd BufNewFile to like so: autocmd BufNewFile \(.*_spec\)\@!.rb 0r ~/vim/skeleton.rb autocmd BufNewFile *_spec.rb 0r ~/vim/skeleton_spec.rb So, I want the first ...
1
vote
1answer
28 views

Rearrange c++ const and reference specifiers

I'm trying to convert const std::string &s to std::string const & s and can't seem to get it quite right. This is the regex I'm using: %s/const \(.\{-}\) &/\1 const & and the ...
1
vote
1answer
47 views

How to search for any repeating character, X number of times

Let's say a buffer has a certain characters I know will be repeated seven (7) times. How can I search for any character repeated seven times? I know I can search for .., but those to characters won'...
0
votes
0answers
28 views

Search and replace any number separated by : trailing with number or alphabet separated by . and / or - or _

Search pattern examples "0:4.10.5-5.el7" "0:0.7.7-13.20120626.ov7" "6:4.14.8-6.po7_3" How to search and print all occurrences like above patterns where the first number can be any number ranging ...
2
votes
1answer
27 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
31 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
85 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
47 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
50 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 ...
2
votes
0answers
111 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
80 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
29 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
49 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
55 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
43 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
19 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
48 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
70 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
26 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
91 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
71 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
89 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
23 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
67 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.