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.

Because regular expressions are not fully standardized, all questions with this tag should also include a tag specifying the applicable programming language or tool.

2050 questions
223
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,603
  • 3
  • 19
  • 17
208
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,334
  • 4
  • 16
  • 20
177
votes
18 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
  • 3,474
  • 7
  • 24
  • 32
162
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,809
  • 2
  • 12
  • 9
121
votes
18 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,415
  • 3
  • 10
  • 8
96
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
  • 961
  • 1
  • 7
  • 3
91
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,093
  • 1
  • 7
  • 10
87
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?
gdoron
  • 990
  • 1
  • 7
  • 10
63
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,775
  • 3
  • 17
  • 12
61
votes
7 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
  • 613
  • 1
  • 5
  • 6
59
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,633
  • 8
  • 31
  • 43
58
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
  • 4,177
  • 16
  • 59
  • 91
52
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,261
  • 3
  • 12
  • 14
47
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
  • 669
  • 2
  • 7
  • 12
44
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
  • 551
  • 1
  • 4
  • 6
1
2 3
99 100