Questions tagged [regex]

Also known as regular expression or regexp, a description of a set of strings, often used for searching and validating strings.

A regex, also known as a regular expression or regexp, is a description of a set of strings with certain properties. For example, \d{5} matches a series of 5 digits, such as 43822.

Use this tag when on the hunt for scripting syntax (in , , , , , etc.) which will match a given set of text.

1870 questions
195
votes
3 answers

How to delete all lines that do NOT contain a certain word in Vim?

In vim I can delete all lines that contain the word "price" with this :g /price/d How can I delete all lines that do NOT contain the word "price"?
digitaljoel
  • 2,323
  • 3
  • 17
  • 17
185
votes
5 answers

How to ignore certain filenames using "find"?

One of my favorite BASH commands is: find . -name '*.*' -exec grep 'SearchString' {} /dev/null \; which searches the contents of all of the files at and below the current directory for the specified SearchString. As a developer, this has come in…
Cody S
  • 2,094
  • 4
  • 15
  • 20
131
votes
4 answers

How to match digits followed by a dot using sed?

I'm trying to use sed to substitute all the patterns with digits followed immediately by a dot (such as 3., 355.) by an empty string. So I try: sed 's/\d+\.//g' file.txt But it doesn't work. Why is that?
Mika H.
  • 1,499
  • 2
  • 10
  • 9
128
votes
16 answers

Removing ANSI color codes from text stream

Examining the output from perl -e 'use Term::ANSIColor; print color "white"; print "ABC\n"; print color "reset";' in a text editor (e.g., vi) shows the following: ^[[37mABC ^[[0m How would one remove the ANSI color codes from the output file? I…
user001
  • 2,814
  • 6
  • 22
  • 30
101
votes
15 answers

Substitution in text file **without** regular expressions

I need to substitute some text inside a text file with a replacement. Usually I would do something like sed -i 's/text/replacement/g' path/to/the/file The problem is that both text and replacement are complex strings containing dashes, slashes,…
Andrea
  • 1,215
  • 3
  • 9
  • 8
90
votes
9 answers

How can I search for regular expressions within webpages using Google Chrome or IE?

How can I search for regular expressions like 'foo|bar' on webpages using Google Chrome or IE? I'm aware of the "Regular Expression Searcher" extension, but it does not work for me. (Nothing happens when I hit slash.) The reviews on the web store…
Sarah
  • 901
  • 1
  • 6
  • 3
86
votes
6 answers

use of alternation "|" in sed's regex

I am using sed, GNU sed version 4.2.1. I want to use the alternation "|" symbol in a subexpression. For example : echo "blia blib bou blf" | sed 's/bl\(ia|f\)//g' should return " blib bou " but it returns "blia blib bou blf". How can I have the…
Cedric
  • 1,033
  • 1
  • 7
  • 9
74
votes
6 answers

Delete files with regular expression

I Tried to delete files that starts with A and ends with 2 numbers but It doesn't do a thing. What I tried: rm ^A*[0..9]2$ Where am I wrong?
61
votes
7 answers

Newlines in sed on Mac OS X

I find that \n doesn't work in sed under Mac OS X. Specifically, say I want to break the words separated by a single space into lines: # input foo bar I use, echo "foo bar" | sed 's/ /\n/' But the result is stupid, the \n is not…
Ivan Xiao
  • 2,635
  • 3
  • 15
  • 12
58
votes
11 answers

Renaming many files in Mac OS X, batch processing

I used to rename file in Linux via a rename command: rename 's/old_pattern/new_pattern/g' *glob Is there something similar in Mac OS X (Snow Leopard)?
math
  • 2,507
  • 7
  • 27
  • 43
56
votes
7 answers

How to run a regex search on Google Chrome or Firefox?

How can I search for expressions like 'foo|bar' on webpages using browsers like Google Chrome or Firefox?
719016
  • 3,659
  • 13
  • 56
  • 88
53
votes
6 answers

Matching only the first occurrence in a line with Regex

I am completely new to regex and I would greatly appreciate any help. The task is simple. I have a CSV file with records that read like…
cows_eat_hay
  • 533
  • 1
  • 4
  • 6
49
votes
6 answers

Is there a linux command like mv but with regex?

For example I want to mv (.*?).sql $1.php, is there a command that lets me specify renaming patterns?
JoshRibs
  • 1,221
  • 3
  • 11
  • 14
43
votes
11 answers

How can I delete everything after the first column in Notepad++?

I'm trying to get rid of everything after a column in Notepad++. Column mode is not an option. Is it possible? What I have: 70.97.110.40 159 ms [n/a] 21 70.97.117.177 134 ms [n/a] …
Bob J
  • 541
  • 1
  • 4
  • 6
42
votes
5 answers

Export all regular expression matches in Textpad or Notepad++ as a list

In Textpad or Notepad++ is there an option to export all the matches for a regular expression find, as a single list? In a big text file, I am searching for tags (words enclosed in % %), using regular expression %\< and \>%, and want all the…
Kiranshell
  • 619
  • 2
  • 7
  • 12
1
2 3
99 100