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

Highlight python raw strings as regular expression

I heard someone had set up their editor to highlight python raw strings as regular expressions, e.g. for Django: urls(r'^site1/(\d)*/new$'', ...) Unfortunately my googleing doesn't get me anything. ...
1
vote
2answers
160 views

How to express “There is a specified symbol in the following unknown numbers of characters” in regex?

I have many python code examples in my markdown files, which should be prepended with >>> symbols. So I write a vim function to do this job: function AddShellPrompt() range let lnum = a:...
4
votes
1answer
122 views

Difference between two end of line signs

Both $ and \n represents the end of a line. To remove an empty line we can use either :g/^$/d or :%s/^\n//g My questions are: What's the difference between these two signs? It seems the first ...
5
votes
4answers
2k views

Run global substitute for each match of a regex, with increment

I've got a somewhat obfuscated javascript file with multiple variables named a##########, where # stands for a single 0-9 digit. Each such identifier may or may not be unique. I was substituting ...
14
votes
2answers
3k views

Regex in Vi: How does one use character classes inside groups? (e.g. [\s\w])

In Vim, the character class \s matches any whitespace character. When I'm using those special characters in groups e.g. [\s\w] it does not match any single whitespace or word character, it does match ...
3
votes
0answers
100 views

Can't seem to syntax-highlight a pattern beginning with a tab [closed]

I have a file with two columns, separated by a tab, and would like to match any part of the second column that begins with an asterisk. For example: RAFP rasp ET *ratchet then *...
2
votes
3answers
68 views

how to avoid highlighting when block commenting

I'm running the command :.,.5s/^/!& to block comment a section of code, however, after running the command the editor highlights the first character in every line of the file. I guess this doesn'...
3
votes
1answer
356 views

Vim ErrorFormat question for Gradle compiler plugin

I am trying to fix a compiler plugin for Gradle. Gradle generates error messages of the type: :compileJava/file/path/Main.java:52: error: cannot find symbol public static ABC abc; ...
10
votes
1answer
671 views

Ignore URLs and email addresses in spell file

Is there a way to have vim not flag HTTP URLs and email addresses as spelling errors? Or, more generally, a way to list regexes of valid spellings?
3
votes
3answers
1k views

Delete all of a file except for certain words that contain certain letters

I have a file that contains words that I want to save, along with other junk that I do not need. I just want to delete everything except the words that contain a certain pattern. Take email addresses ...
8
votes
2answers
225 views

How can I make vim stop splitting my words into chunks?

English language users probably don't have this trouble, but vim is pretty dumb when it comes to regular expressions. In my case most letters in the Turkish alphabet outside the EN alphabet don't seem ...
3
votes
2answers
1k views

Insert a pattern before another one

I have a line looking like this: pattern1 any text pattern2 any text pattern2 any text pattern2 any text And I want it to become: pattern1 any text pattern1 pattern2 any text pattern1 pattern2 any ...
16
votes
1answer
4k views

Can I search for a Unicode combining character in Vim?

I have a file with the character ã (lowercase a + combining tilde). Encoding and fileencoding are both utf-8. ga shows <a> 97, hex 61, octal 141 <~> 771, Hex 0303, Octal 1403 (but ...
11
votes
1answer
6k views

Deleting a range of n lines before and after a matched line?

I have a repetitive configuration file and I'd like to match a regex and delete a range of lines before, and after a match. I'd also like delete the match in one command. I can use... :g/match/-1d ...
11
votes
1answer
2k views

Sort based on comma separated words

Normal command :sort can sort lines based on column or virtual column (\%c or \%v), could the higher level logical column be used as sorting key? Using regular expression looks a little complex for ...
7
votes
1answer
22k views

Search for lines starting with given string in vim?

I have this code snippet: # import ipdb; ipdb.set_trace() def foo(): ... import ipdb; ipdb.set_trace() ... # import ipdb; ipdb.set_trace() How should I specify search query, that it ...
4
votes
1answer
887 views

Change '\r' to '\n' for search and replace?

In expression :s/foo/bar\n/g When replacing \n inserts a null byte, according to the docs I have to use \r. Since I insert newlines more frequently than null bytes, is there a way to change this ...
3
votes
2answers
682 views

Arithmetic in Regular Expression?

I have a log file for what should be simple counter values, formatted as: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 etc... I'm looking for a way to find if there's anywhere in ...
3
votes
2answers
2k views

How to replace specific lines with regex?

Suppose I have a file that contains: Time: 60 minutes Seconds: 3600 Milliseconds: 36000 I would like to replace any word with seconds. %s/seconds/time/g What am looking for is after the replace, ...
5
votes
4answers
7k views

How to delete part of a line that does not match a pattern?

I'd like to remove everything apart of my pattern match. For example I've the following line: window.page4_callback(["here \"is\" some text"]); and I'd like to leave the text only within the double ...
25
votes
3answers
64k views

Find and replace using regular expressions

I have a file with a bunch of user defaults in. I want to change some of the text, but I'm struggling coming up with a matcher and replacer. Using the following example: ##############################...
3
votes
2answers
1k views

Check if value matches any regex in a list

I have encountered some situations where I need to check if a value matches a regex in a list of regexes. For instance, say I have a list of regexes which match filetypes: ['gundo', 'nerdtree', 'git*...
21
votes
2answers
2k views

Is lookaround possible in vim's regex system?

I have encountered many situations in vim where it would be useful to have lookaround statements. I have tried using the standard lookaround syntax ((?=), (?!) etc ..), but it appears vim does not ...
3
votes
1answer
5k views

searching duplicate words

This page says that I can find duplicate words using a regular expression. The suggested command is \(\<\w\+\>\)\_s*\<\1\> What does "\w" mean here? Suppose the target pattern is "123\...
3
votes
1answer
154 views

How can I edit a regular expression search string as if it were a file?

Sometimes a regular expression starts to get pretty complex, and it's hard to edit in Vim's commandline. It seems the obvious way is to edit the search term as though it's a file and be able to ...
6
votes
2answers
2k views

How to write condition with regular expression?

I'm aware of the following comparison: if @% == "/tmp/crontab.zi5NeVPGRc" set nobackup set nowritebackup set noundofile set noswapfile endif But how can I use regular expression? For example ...
3
votes
1answer
188 views

How do I cut a single large file into manageable chunks by regex?

I currently have a single file that has nearly 10000 lines of text in it. Each section that I want is broken up like so - Chapter 1: Chapter 1 TEXT GOES HERE Chapter 2: Chapter 2 TEXT GOES HERE ...
9
votes
1answer
2k views

Using a variable in a regex pattern [duplicate]

I'm trying to write a function that will delete each line that matches a pattern. But that pattern will change based on the words held in a list (made from a dictionary). I was thinking it would be a ...
3
votes
3answers
2k views

regex: how to replace “\[”?

I have this sample line in the buffer: export PS1="\[${Cyan}\]$(((SHLVL>1)) && echo "${SHLVL}\[${IBlack}\].")\[${IGreen}\]\u\[${IBlack}\]@\[${Purple}\]\h\[${IYellow}\] \w \$ \[${Color_Off}\...
5
votes
2answers
496 views

Entering search pattern with syntax highlighting?

A search pattern used with /, entered on the vim command line, can be very complex. But even for a normal regular expression pattern, syntax highlight of the expression syntax would be helpful. ...
2
votes
2answers
449 views

Why are ^ and $ still magic in nomagic mode?

In 'nomagic' mode, ^ and $ have a special meaning and no others do. Why were these selected, rather than making people type \^ and \$ just as they do for \. and \*? EDIT: I have been able to confirm ...
15
votes
1answer
1k views

Are Vim's regex magics compatible with well-known regex classes?

Many Unix tools' regular expression syntaxes are often the POSIX-codified Basic and Extended Regular Expressions (BRE and ERE, respectively), and, in some modern implementations, Perl-style (PCRE ...
4
votes
1answer
191 views

Duplicating line content by substitution

I'm trying to vimify a task of editing bulk file move commands. I'm using Fossil-SCM to manage a bunch of files and have some changes in directory structure. To get SCM to grasp the move, I have to ...
13
votes
1answer
1k views

Folding by regex search pattern

I got a plain text file with whitespace separated columns of values. Like this: AU 3030 .... ... .... AU 3031 .... ... .... AU 3032 .... ... .... AU 3033 .... ... .... IT 48100 ... .. ..... IT ...
1
vote
1answer
1k views

How to replace URL resource path with regex?

I am trying to write a regular expression that will match a link-attribute in .png files. Example: I have a lot of .png like this https://jnTb6EZom8c/s144/800px-s%25252Ct%252529%2520%25282%2529.png ...
5
votes
1answer
951 views

How do I parse URL's from the text

I have a multiline text file. Some lines have URL's. What would be the most efficient way to extract those URL's? I wrote macros and did a bunch of unnecessary things. Now I'm thinking about regex ...
9
votes
1answer
804 views

Apply normal mode command to regex matches

I am using the following regular expression to match a documentation string underneath a Clojure function definition: \vdefn.*\n\s*\zs"([^"]|\n)*" Is there a way to run the normal mode gq (format ...
3
votes
1answer
8k views

Regex to search double quoted strings on multiple lines

I am trying to write a regular expression that will match all double quoted strings across newlines. I have been fiddling around with the following pattern but it seems to only match strings on the ...
5
votes
1answer
80 views

Programming-oriented word swap using transwrd.vim

The plugin transwrd.vim allows to to easily swap two words by pressing <A-t> while having cursor on the latter word. For example: two words becomes words two. I think the pattern which ...
9
votes
1answer
467 views

Regex that prefers shorter matches within a match? (This is more involved than non-greedy)

I'm trying to create a script for performing fuzzy search inside a buffer. The main idea is to take some input, and insert .\{-} between every pair of characters, e.g. foo becomes f.\{-}o.\{-}o. This ...
6
votes
2answers
2k views

How do I search for a line that contains a pattern at column n

In writing some Vimscript to try to answer this question, I was exploring ways to "search for a line with pattern P in position X" (P is a search pattern, X is a number). One way to do this would be ...
14
votes
3answers
6k views

How to make regex matchers non-greedy?

I am trying to use a regex for replacing text in a file (replace a full url with just protocol/domain/): :%s/\(https\?:\/\/.*?\/\).*/\1/gc Unfortunately .*? does not match the string, even trying to ...
4
votes
1answer
94 views

Custom syntax file for FIX data doesn't work

I tried to create a custom syntax file to read FIX messages. Note: I am certain my custom files are being loaded by Vim. The file format is sequences of NUMBER=VALUE pairs separated by the ...