login about faq

What syntax do the Egrep regular expressions use?

asked Jan 27 '11 at 10:05

SSH%20KB's gravatar image

SSH KB ♦
509249246237


Egrep syntax

Egrep Patterns

The escape character is a backslash (\). With this you can escape meta characters which you would like to use in their plain character form. In the following examples literal 'E' and 'F' denote any expression, be it a pattern or a character.

( Start a capturing subexpression.

) End a capturing subexpression.

E|F Disjunction, match either E or F (inclusive). E is preferred if both match.

E* Act as Kleene star, match E zero or more times.

E+ Closure, match E one or more times.

E? Option, match E optionally once.

. Match any character except for newline characters (\n, \f, \r) and the NULL byte.

E{n} Match E exactly n times.

E{n,} or E{n,0} Match E n or more times.

E{,n} or E{0,n} Match E at most n times.

E{n,m} Match E no less than n times and no more than m times.

[ Start a character set, see Section Character Sets for Egrep.

$ Match the empty string at the end of the input or at the end of a line.

^ Match the empty string at the start of the input or at the beginning of a line.

Escaped Tokens for Regex Syntax Egrep

\0n..n The literal byte with octal value n..n.

\0 The NUL byte.

[1-9]..x The literal byte with decimal value [1-9]..x.

\xn..n or \0xn..n The literal byte with hexadecimal value n..n.

\< Match the empty string at the beginning of a word.

\> Match the empty string at the end of a word.

\b Match the empty string at a word boundary.

\B Match the empty string provided it is not at a word boundary.

\w Match a word-constituent character, equivalent to [a:zA:Z0:9-].

\W Match a non-word-constituent character.

\a Literal alarm character.

\e Literal escape character.

\f Literal line feed.

\n Literal new line, equivalent to C's \n so can be more than one character long.

\r Literal carriage return.

\t Literal tab.

All other escaped characters denote the literal character itself.

Character Sets for Egrep

A character set starts with '[' and ends at non-escaped ']' that is not part of a POSIX character set specifier and that does not follow immediately after '['.

The following characters have a special meaning and need to be escaped if meant literally:

- (minus sign) A range operator, except immediately after '[', where it loses its special meaning.

^ If immediately after the starting '[', denotes a complement: the whole character set will be complemented. Otherwise literal. '^'.

[:alnum:] Characters for which 'isalnum' returns true (see ctype.h).

[:alpha:] Characters for which 'isalpha' returns true (see ctype.h).

[:cntrl:] Characters for which 'iscntrl' returns true (see ctype.h).

[:digit:] Characters for which 'isdigit' returns true (see ctype.h).

[:graph:] Characters for which 'isgraph' returns true (see ctype.h).

[:lower:] Characters for which 'islower' returns true (see ctype.h).

[:print:] Characters for which 'isprint' returns true (see ctype.h).

[:punct:] Characters for which 'ispunct' returns true (see ctype.h).

[:space:] Characters for which 'isspace' returns true (see ctype.h).

[:upper:] Characters for which 'isupper' returns true (see ctype.h).

[:xdigit:] Characters for which 'isxdigit' returns true (see ctype.h).

Example: [[:xdigit:]XY] is typically equivalent to [0123456789ABCDEFabcdefXY] .

It is also possible to include the predefined escaped character sets into a newly defined one, so

[\d\s]

matches digits and whitespace characters.

Also, escape sequences resulting in literals work inside character sets.

link

answered Jan 27 '11 at 10:13

SSH%20KB's gravatar image

SSH KB ♦
509249246237

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×1
×1

Asked: Jan 27 '11 at 10:05

Seen: 6,738 times

Last updated: Feb 10 '11 at 10:22

Related questions

All user contributed content licensed under the cc-by-sa license.
Powered by OSQA.