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.

2145 questions
248
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,863
241
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,684
209
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,994
175
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,939
136
votes
20 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,555
113
votes
5 answers

How do I search a page using a regular expression in Firefox?

I want to use regular expressions to search a page in Firefox. Is this possible?
BrunoLM
  • 5,982
104
votes
8 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
  • 1,041
98
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
  • 1,100
93
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,123
66
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,945
65
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…
63
votes
6 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,683
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,693
54
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,281
52
votes
7 answers

How should I write a regex to match a specific word?

I've been trying to get a specific regex working but I can't get it to do what I need. Basically, I want it to look for ROCKET. The regex should match ROCKET in upper or lower cases, and with or without punctuation, but not when part of another…
Kefka
  • 1,536
  • 2
  • 17
  • 32
1
2 3
99 100