Skip to content

added regular expressions #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Currently, there are these translations of **bash-handbook**:
- [Local variables](#local-variables)
- [Environment variables](#environment-variables)
- [Positional parameters](#positional-parameters)
- [Regular Expressions](#regular-expressions)
- [Shell expansions](#shell-expansions)
- [Brace expansion](#brace-expansion)
- [Command substitution](#command-substitution)
Expand Down Expand Up @@ -269,6 +270,121 @@ Variables may also have _default_ values. We can define as such using the follow
FOO=${FOO:-'default'}
```

# Regular expressions

They are a powerful tool for manipulating and searching text. Here are some examples of regular expressions that use each `metacharacter`:

### `.` (dot)
Matches any single character except newline.
```bash
grep h.t file.txt
```
Output:
```bash
hat
hot
hit
```

### `*` (asterisk)
Matches zero or more occurrences of the preceding character or group.
```bash
grep ab*c file.txt
```
Output:
```bash
ac
abc
abbc
abbbc
```

### `+` (plus)
Matches one or more occurrences of the preceding character or group.
```bash
grep ab+c file.txt
```
Output:
```bash
abc
abbc
abbbc
abbbbc
```

### `?` (question mark)
Matches zero or one occurrence of the preceding character or group.
```bash
grep ab?c file.txt
```
Output:
```bash
ac
abc
```

### `|` (pipe)
Matches either the pattern to the left or the pattern to the right.
```bash
egrep "cat|dog" file.txt
```
Output:
```bash
cat
dog
```

### `[]` (character class)
Matches any character inside the brackets.
```bash
[aeiou] will match any vowel
[a-z] will match any lowercase letter
```

### `[^]` (negated character class)
Matches any character not inside the brackets.
```bash
[^aeiou] will match any consonant
[^a-z] will match any non-lowercase letter
```

### `()` (grouping)
Groups multiple tokens together and creates a capture group.
```bash
egrep "(ab)+" file.txt
```

Output:
```bash
ab
abab
ababab
```

### `{}` (quantifiers)
Matches a specific number of occurrences of the preceding character or group.
```bash
egrep "a{3}" file.txt
```

Output:
```bash
aaa
aaaa
aaaaa
```

### `\` (escape)
Escapes the next character to match it literally.
```bash
egrep "a\+" file.txt
```

Output:
```bash
a+
```

# Shell expansions

_Expansions_ are performed on the command line after it has been split into _tokens_. In other words, these expansions are a mechanism to calculate arithmetical operations, to save results of commands' executions and so on.
Expand Down