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
15
votes
1answer
3k 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
5k 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
21k 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
841 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
666 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
1k 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
6k 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 ...
22
votes
3answers
59k 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
186 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
489 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
419 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 ...
14
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
190 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 ...
11
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
844 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
742 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
7k 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
79 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
438 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 ...
12
votes
3answers
5k 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 ...