Questions tagged [regular-expression]

Regular expressions are a means of matching a pattern of characters within a string.

Regular expressions (regex or regexp for short) are patterns that describe a set of strings based on simple syntactic criteria.

Tools such as , and involve regular expression matching as a key feature. Most programming languages include a regular expression library, and most text editors provide regular expression search and replacement.

Shell for file names (also called glob patterns) are a different, less powerful syntax (though ksh, bash and zsh have more powerful extensions). Bash, ksh93 and zsh's =~ operator offer regex matching.

External reference

Further reading

2609 questions
13 answers

How do I grep for multiple patterns with pattern having a pipe character?

I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing grep (foo|bar) *.txt but the shell interprets the | as a pipe and complains when bar isn't an executable. How can I…
  • 9,122
  • 5
  • 24
  • 38
8 answers

Can grep output only specified groupings that match?

Say I have a file: # file: 'test.txt' foobar bash 1 bash foobar happy foobar I only want to know what words appear after "foobar", so I can use this regex: "foobar \(\w\+\)" The parenthesis indicate that I have a special interest in the word right…
Cory Klein
  • 17,221
  • 26
  • 78
  • 92
18 answers

How can I use sed to replace a multi-line string?

I've noticed that, if I add \n to a pattern for substituting using sed, it does not match. Example: $ cat > alpha.txt This is a test Please do not be alarmed $ sed -i'.original' 's/a test\nPlease do not/not a test\nBe/' alpha.txt $ diff…
Belmin Fernandez
  • 8,767
  • 14
  • 44
  • 49
5 answers

What is the difference between `grep`, `egrep`, and `fgrep`?

Can anyone tell me the technical differences between grep, egrep, and fgrep and provide suitable examples? When do I need to use grep over egrep and vice versa?
Vishwanath Dalvi
  • 4,256
  • 5
  • 18
  • 17
5 answers

How to use find command to search for multiple extensions

I can get all jpg images by using: find . -name "*.jpg" But how can I add png files to the results as well?
  • 3,303
  • 3
  • 16
  • 8
7 answers

Using sed to find and replace complex string (preferrably with regex)

I have a file with the following contents: and I need to make a script that changes the "name" in the first line to "something", the…
Harry Muscle
  • 2,187
  • 2
  • 12
  • 7
9 answers

How to run grep with multiple AND patterns?

I would like to get the multi pattern match with implicit AND between patterns, i.e. equivalent to running several greps in a sequence: grep pattern1 | grep pattern2 | ... So how to convert it to something like? grep pattern1 & pattern2 &…
  • 5,806
  • 16
  • 52
  • 65
1 answer

Why does my regular expression work in X but not in Y?

I wrote a regular expression which works well in a certain program (grep, sed, awk, perl, python, ruby, ksh, bash, zsh, find, emacs, vi, vim, gedit, …). But when I use it in a different program (or on a different unix variant), it stops matching.…
Gilles 'SO- stop being evil'
  • 766,554
  • 187
  • 1,586
  • 2,083
2 answers

Grep 'OR' regex problem

I am trying to use grep with a regex to find lines in a file that match 1 of 2 possible strings. Here is my grep: $ grep "^ID.*(ETS|FBS)" my_file.txt The above grep returns no results. However if I execute either: $ grep "^ID.*ETS" my_file.txt …
  • 1,679
  • 3
  • 14
  • 11
2 answers

Find files with certain extensions

How can I use find to find all files that have a .xls or .csv extension? I have seen a -regex option but I don't know how to use it.
7 answers

Find files in multiple folder names

I am trying to list all the files from dir1, dir2, dir3 and dir4 which might be anywhere in as a sub directory of my cwd using the find command. I tried the following with no success: find . -type f -regextype posix-egrep -regex…
  • 1,417
  • 4
  • 13
  • 15
4 answers

grep lines starting with "1" in Ubuntu

I try to search for lines that start with "1" using ls -1 | grep ^1* but it returns lines that do not start with 1. What I am missing here?
  • 92,534
  • 165
  • 512
  • 896
5 answers

Vim - how to replace one new line \n with two \n's

In vim editor, I want to replace a newline character (\n) with two new line characters (\n\n) using vim command mode. Input file content: This is my first line. This is second line. Command that I tried: :%s/\n/\n\n/g it replaces the string with…
  • 912
  • 1
  • 6
  • 13
10 answers

Checking if an input number is an integer

I'm trying to check if an input is an integer and I've gone over it a hundred times but don't see the error in this. Alas it does not work, it triggers the if statement for all inputs (numbers/letters) read scale if ! [[ "$scale" =~ "^[0-9]+$" ]] …
  • 1,903
  • 3
  • 15
  • 13
6 answers

How to diff files ignoring comments (lines starting with #)?

I've two configuration files, the original from the package manager and a customized one modified by myself. I've added some comments to describe behavior. How can I run diff on the configuration files, skipping the comments? A commented line is…
  • 19,253
  • 17
  • 69
  • 110
2 3
99 100