Linux 正規表現チュートリアル: Grep 正規表現の例
⚡ スマートサマリー
Linux regular expressions are special character sequences that search data and match complex patterns across tools such as grep, sed, and vi, using basic, interval, extended, and brace-expansion forms to filter text precisely.

Regular expressions are one of the most powerful features of Linux text tools, letting you match, filter, and transform data with compact patterns. This page explains the basic, interval, and extended forms, plus brace expansion, with worked grep examples.
Linux の正規表現とは何ですか?
Linux regular expressions are special characters that help search data and match complex patterns. Regular expressions are shortened as ‘regexp’ or ‘regex’. They are used in many Linux programs such as grep, bash, rename, sed, and more.
正規表現の種類
For ease of understanding, let us learn the different types of regular expressions one by one. This page covers basic, interval, and extended regular expressions, and finishes with brace expansion.
基本的な正規表現
Some of the commonly used commands that work with regular expressions are tr, sed, vi, and grep. The table below lists some of the basic regular expressions.
| シンボル | 詳細説明 |
|---|---|
| . | Replaces any character |
| ^ | Matches the start of a string |
| $ | Matches the end of a string |
| * | Matches zero or more times of the preceding character |
| \ | Represents special characters |
| () | グループ正規表現 |
| ? | Matches exactly one character |
Let us see an example. First, run cat sample to display the contents of an existing file, as shown below.
Next, search for content containing the letter ‘a’. The screenshot below shows grep returning every line that includes an ‘a’.
The caret ‘^’ matches the start of a string. The example below searches for content that starts with the letter ‘a’.
Only lines that start with the character are filtered. Lines that do not contain the character ‘a’ at the start are ignored.
Let us look at another example. The screenshot below again shows the sample file contents before filtering by the end of a line.
Select only those lines that end with the letter ‘t’ by using the ‘$’ anchor, as shown below.
間隔の正規表現
These expressions specify the number of occurrences of a character in a string. They are listed below.
| 表現 | 詳細説明 |
|---|---|
| {NS} | 「n」回出現する直前の文字と正確に一致します |
| {n、m} | 「n」回出現する前にある文字と一致しますが、m 回を超えません |
| {n、} | 前の文字が「n」回以上出現する場合にのみ一致します。 |
For example, filter out all lines that contain the character ‘p’. The screenshot below shows the matching lines.
Now check that the character ‘p’ appears exactly two times in a string, one after the other. The syntax for this is:
cat sample | grep -E p\{2}
The result of the interval match is shown below.
注: これらの正規表現には -E を追加する必要があります。
拡張正規表現
These regular expressions combine more than one expression. Some of them are:
| 表現 | 詳細説明 |
|---|---|
| \+ | 前の文字の XNUMX つ以上の出現に一致します |
| \? | 前の文字の XNUMX 回または XNUMX 回の出現に一致 |
For example, search for all occurrences of the character ‘t’. The screenshot below shows the result.
Suppose you want to filter out lines where the character ‘a’ precedes the character ‘t’. You can use a command like the one below:
cat sample|grep "a\+t"
The output is shown in the following screenshot.
ブレースの拡張
The syntax for brace expansion is either a sequence or a comma-separated list of items inside curly braces ‘{}’. The starting and ending items in a sequence are separated by two periods ‘..’.
Some examples are shown below.
In the examples above, the echo command creates strings using brace expansion, producing multiple strings out of a single pattern.










