Detailed explanation of using regular expressions in linux
这次给大家带来在linux里使用正则表达式详解,在linux里使用正则表达式的注意事项有哪些,下面就是实战案例,一起来看一下。
前言
正则表达式应用广泛,在绝大多数的编程语言都可以完美应用,在Linux中,也有着极大的用处。
使用正则表达式,可以有效的筛选出需要的文本,然后结合相应的支持的工具或语言,完成任务需求。
在本篇博客中,我们使用grep/egrep来完成对正则表达式的调用,其实也可以使用sed等工具,但是sed的使用极大的需要正则表达式,为了在后面sed篇的书写,就只能这样排序了,有需要的朋友可以把这两篇一起来看。
正则表达式的类型
正则表达式可以使用正则表达式引擎实现,正则表达式引擎是解释正则表达式模式并使用这些模式匹配文本的基础软件。
在Linux中,常用的正则表达式有:
- POSIX 基本正则表达式(BRE)引擎
- POSIX 扩展正则表达式(BRE)引擎
基本正则表达式的基本使用
环境文本准备
1 2 3 4 5 |
|
纯文本
纯文本可以完全匹配对应的单词,需要注意的有正则表达式模式严格区分大小写。
1 2 3 4 |
|
在正则表达式中,不必局限于完整的单词,所定义的文本出现在数据流的任意位置,正则表达式都将匹配。
1 2 3 4 |
|
当然也不必局限于单独的单词,也可以在文本字符串中出现空格和数字。
1 2 |
|
在正则表达式模式中使用文本字符串时,有一个问题需要注意。
在正则表达式中定义文本字符串时有几个例外,正则表达式赋予了它们特殊的含义,如果在文本中使用这些特殊字符,有可能得不到预期的效果。
正则表达式认可的特殊字符:
复制代码 代码如下:
.*[]^${}+?|()
如果想要使用这些特殊字符作为普通的文本字符,就需要转义(escape)它,即是在该字符前添加一个特殊字符,向正则表达式引擎说明:它应该将下一个字符解释为普通文本字符。
实现该功能的特殊字符是:“\”反斜杠字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
定位符
从头开始
脱字符(^)尖角号定义从数据流中文本行开头开始的模式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
查找结尾
美元符号$特殊字符定义结尾定位,在文本模式之后添加这个特殊字符表示数据行必须以此文本模式结束。
1 2 3 4 5 6 7 8 |
|
联合定位
比较常用的就是“^$” 表示空行
结合“^#”,由于#在Linux代表注释
输出该文本的有效配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
字符出现范围
{n,m} //前一个字符出现了n到m次
{n,} //前一个字符出现了n次以上
{n} //前一个字符出现了n次
1 2 3 4 |
|
点字符
点特殊字符用于匹配除换行符之外的任意单个字符,但点字符必须匹配一个字符;如果在圆点位置没有字符,那么模式匹配失败。
1 2 3 4 5 6 |
|
字符类
字符类可以定义一类字符来匹配文本模式中的某一位置。如果在字符类中的某一字符在数据流中,就和模式匹配。
为定义字符类,需要使用方括号。应该将要包括在该类中的所有字符用方括号括起来,然后模式中使用整个字符类,就像任意的其他通配符一样。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
如果不能确定某个字符的大小写,就可以使用该模式:
1 2 3 4 |
|
在单个表达式内可以使用多个字符类:
1 2 3 4 |
|
字符类对数字同样支持:
1 2 3 4 5 6 |
|
字符类还有一种极为常见的用途是解析可能拼错的单词:
1 2 |
|
否定字符类
用于查找不在该字符类中的字符,只需在字符类范围的开头添加脱字符(^).
即使使用否定,字符类仍必须匹配一个字符。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
使用范围
当你需要匹配的字符很多并且有一定规律时,可以这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
特殊字符类
用于匹配特定类型的字符。
[[:blank:]] 空格(space)与定位(tab)字符
[[:cntrl:]] 控制字符
[[:graph:]] 非空格(nonspace)字符
[[:space:]] 所有空白字符
[[:print:]] 可显示的字符
[[:xdigit:]] 十六进制数字
[[:punct:]] 所有标点符号
[[:lower:]] 小写字母
[[:upper:]] 大写字母
[[:alpha:]] 大小写字母
[[:digit:]] 数字
[[:alnum:]] 数字和大小写字母
星号
在某个字符之后加一个星号表示该字符在匹配模式的文本中不出现或出现多次
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
扩展正则表达式
问号
问号表示前面的字符可以不出现或者出现一次。不匹配重复出现的字符。
1 2 3 |
|
加号
加号表示前面的字符可以出现一次或者多次,但必须至少出现一次,该字符若是不存在,则模式不匹配。
1 2 3 4 5 6 7 |
|
使用大括号
使用大括号指定对可重复的正则表达式的限制,通常称为间隔。
- m:该正则表达式正好出现m次
- m,n:该正则表达式出现最少m次,最多n次
1 2 3 4 |
|
正则表达式实例
这里有一个实例,对基本的正则表达式进行了练习和实例。
因为正则表达式,单看概念或者理论还是比较简单的,然而在实际的使用中,却不是那么好用,一旦用好了,对效率的提升绝对时可观的。
1.过滤下载文件中包含 the 关键字
1 |
|
2.过滤下载文件中丌包含 the 关键字
1 |
|
3.过滤下载文件中丌论大小写 the 关键字
1 |
|
4.过滤 test 或 taste 这两个单字
1 2 |
|
5.过滤有 oo 的字节
1 |
|
6.过滤丌想要 oo 前面有 g 的
1 2 |
|
7.过滤 oo 前面丌想有小写字节
1 |
|
8.过滤有数字的那一行
1 |
|
9.过滤以 the 开头的
1 |
|
10.过滤以小写字母开头的
1 |
|
11.过滤开头丌是英文字母
1 |
|
12.过滤行尾结束为小数点.那一行
1 |
|
13.过滤空白行
1 |
|
14.过滤出 g??d 的字串
1 |
|
15.过滤至少两个 o 以上的字串
1 2 |
|
16.过滤 g 开头和 g 结尾但是两个 g 之间仅存在至少一个 o
1 |
|
17.过滤任意数字的行
1 |
|
18.过滤两个 o 的字串
1 |
|
19.过滤 g 后面接 2 到 5 个 o,然后在接一个 g 的字串
1 |
|
20.过滤 g 后面接 2 个以上 o 的
1 |
|
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Detailed explanation of using regular expressions in linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)
