Regular expressions
A regular expression (or short: regex) is a pattern that is used to identify certain lines or parts of character-strings.
Regular Expressions (or regex) are a basic concept of the information technology. They let you define parts of strings. These definitions can have variable parts. This makes them so interesting.
Many unix-programs and programming languages use regex for searching (aka pattern matching) and, if applicable, for replacing. The following list is far from complete: vi, grep, sed, Perl, PHP, and many more.
As you can see in the table below, the regular expression syntax is quite similar in grep, sed and Perl. Bash has a much more limited syntax, and uses different characters for the same function (? vs. .) and the same character for a slightly different function (* vs. .*). This probably has to do with the usefullness of the dot in filenames, not only to mark file-extensions.
Regular expressions are also used in text-editors or other applications for searching and replacing. The syntax may vary from program to program, but the basic functions remain the same. (feel free to add your favorite application :)
| match… |
Bash-glob |
grep |
sed |
Perl |
| …any one character |
? |
. |
| …any characters (or no character) |
* |
.* (see repetition) |
| …one character from a set |
[ ] |
| …anything but the given set |
[^ ]
|
| …ranges in the set (e.g. a to z) |
[a-z] |
| repetitions |
Bash-glob |
grep |
sed |
Perl |
| zero or more of the preceding match |
n.a. |
* |
| zero or one of the preceding match |
n.a. |
\? |
? |
| one or more of the preceding match |
n.a. |
\+ |
+ |
| exactly x times |
n.a. |
\{x\} |
{x} |
| x times or more |
n.a. |
\{x,\} |
{x,} |
| x through y times |
n.a. |
\{x,y\} |
{x,y} |
| positions |
Bash-glob |
grep |
sed |
Perl |
| Beginning of line |
Beg. of expr. |
^
|
| End of line |
End of expr. |
$ |
| Word boundary |
' ' |
\b |
Links
Examples
- In this UGU-admin-tip, there are regexs to match the hidden files. Since ‘.*’ is not good (it also matches ‘..’ and thus goes up in the directory-hirarchy when used in recursive commands), my suggestion is be ‘.[^.]*’ (shell-glob) or ‘^\.[^\.]’ (sed, grep and the like). This matches everything that starts with a dot and is followed by a ‘non-dot’.
Copyright (c) by the authors.
Prior to editing, authors agreed to license their contributions by the terms of the GPL.
See our licensing page for details.
Linux® is a registered trademark of Linus Torvalds.
Welcome to LinuxBasics.org - The online community that helps people to get Linux installed and running.
During this tour, we will guide you through our website, which has many facets which wait to be explored
The biggest project we are running is our Linux course, based on the LBook.
The book is stored in wiki-format, which enables us to update and correct it as we go.
Discussion for the course is on our Forum
Our Forum is used for discussion of Linux and for questions and answers.
Search the mailing-list that was used prior to the Forum.
The questions and answers from the list are stored in the list's archives in order to help others with the same problems.
Every weekend, we meet to chat in IRC. These meetings are NOT mandatory, but are a nice chance to get to know each other better.
IRC is also a great tool to solve many problems, since it is very quick and easy to ask for more details if you need them.
The tutorials are one of the oldest sections on the LBo-website.
Here you find explanations on how to do specific tasks in Linux. Many of the tutorials were created after a certain problem
has been discussed (and usually solved :) on the mailing-list.
The tutorials are categorized in
In the links section, you find outbound links to other valuable resources.
One of our later additions to the site. We maintain a mirror of the Linux Documentation Project. This is our contribution to the "home of the HOWTOs"
Another later addition is the LBlog which focuses on how to do stuff on the Linux Desktop. It begins with the basics on installing Ubuntu.
Using the integrated site-search, you can search the tutorials, the LBook and all other wiki-pages
Simply type the search term into the box in the upper-right corner of our webpages
As a community, we depend on your feedback and collaboration. So, if you have something to share with others, please contact us. If you have a suggestion for a topic you would like to see covered here, please add it on the Wishlist.
There are many ways to contribute: You can answer questions on the Forum, you can write a complete tutorial or just a step-by-step documentation on how you completed a specific task using linux. Ask questions if the information on this site is not clear, tell us if we got something wrong, spell-check our writings, whatever.
We are looking forward to meeting you at LinuxBasics.org
Anita, Jisao, Sam and Stefan