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, shortened to regexp or regex, describe text patterns for searching and matching.
  • 🧩 基本セット: Symbols such as . ^ $ * and \ anchor, repeat, and escape characters in a pattern.
  • 🔢 間隔: Braces such as {n} and {n,m} control how many times the preceding character repeats.
  • 拡張: With grep -E, \+ and \? match one-or-more and zero-or-one occurrences of a character.
  • 🧵 Brace expansion: Curly braces generate multiple strings from a sequence or a comma-separated list.
  • 🛠️ Where used: Tools including grep, sed, tr, and vi rely on regular expressions to filter text.
  • 🤖 AIによる支援: AI assistants and GitHub Copilot can generate, explain, and debug regex patterns quickly.

Linux regular expressions with grep — basic, interval, and extended regex examples

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.

cat sample command displaying the contents of an existing file in the terminal

Next, search for content containing the letter ‘a’. The screenshot below shows grep returning every line that includes an ‘a’.

grep searching the sample file for lines that contain the letter a

The caret ‘^’ matches the start of a string. The example below searches for content that starts with the letter ‘a’.

grep with a caret anchor matching only lines that start 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.

Sample file contents shown before filtering lines that end with the letter t

Select only those lines that end with the letter ‘t’ by using the ‘$’ anchor, as shown below.

grep with a dollar anchor selecting only lines that end with the letter t

間隔の正規表現

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.

grep filtering the sample file for lines that contain the character p

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.

grep -E interval expression matching the character p appearing exactly twice

注: これらの正規表現には -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.

grep searching the sample file for all lines that contain the character t

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.

grep extended expression matching one or more a characters followed by t

ブレースの拡張

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.

echo command using brace expansion to generate multiple strings from a sequence and a list

In the examples above, the echo command creates strings using brace expansion, producing multiple strings out of a single pattern.

よくあるご質問

grep uses basic regular expressions by default. egrep enables extended expressions (the same as grep -E), and fgrep matches fixed strings with no regex (grep -F). Modern GNU grep marks egrep and fgrep as deprecated, so prefer grep -E and grep -F.

Character classes match a category of characters. Bracket expressions like [a-z] match a range, while POSIX classes such as [[:alpha:]], [[:digit:]], and [[:space:]] match letters, numbers, and whitespace. They keep patterns readable and portable across different locales.

Add the -i option, for example grep -i “hello” sample. This matches Hello, HELLO, and hello alike. Without -i, regular expressions are case-sensitive, so an uppercase letter and its lowercase form are treated as two different characters.

Shell wildcards (globbing) expand filenames, where * means any characters and ? means one character. Regular expressions match text inside files, where * means zero or more of the preceding character. The same symbols mean different things in each context.

Use the -o option, for example grep -o “a\+t” sample. Instead of printing the entire line, grep prints only the part that matches the pattern, one match per line. It is useful for extracting values such as emails or numbers.

Use alternation with the pipe symbol. In extended mode, grep -E “cat|dog” sample matches lines containing cat or dog. In basic mode you must escape it as grep “cat\|dog” sample. Alternation lets one command search for several patterns at once.

AI assistants can turn a plain-English description into a working regex, explain what a cryptic pattern does, and suggest fixes when a match fails. Machine-learning tools also detect recurring patterns in large log files that would be tedious to write by hand.

Yes. GitHubコパイロット suggests grep commands and regular expressions directly from a comment or a partial pattern as you type. It speeds up writing complex expressions, though you should test any generated pattern against real sample data before relying on it.