首页 web前端 js教程 Linux中正则表达式使用详解

Linux中正则表达式使用详解

May 25, 2018 am 10:14 AM
linux 表达式 详解

这次给大家带来Linux中正则表达式使用详解,Linux中正则表达式使用的注意事项有哪些,下面就是实战案例,一起来看一下。

1、组成

普通字符:普通字符串,没有特殊含义
特殊字符:在正则表达式中具有特殊的含义
正则表达式中常见的meta字符【特殊字符】

2、POSIX BRE【基本】与ERE【扩展】中都有的meta字符

\ :通常用于打开或关闭后续字符的特殊含义,如(...)【\是转义字符,去掉符号的特殊意义,()、{}等在shell中都有特殊的意义】
.和以及.的区别:

[root@localhost ~]# cat -n test.txt
     1  gd
     2  god
     3
     4  good
     5  goood
     6  goad
     7
     8  gboad

2.1、. :匹配任意单个字符(除null,即不能为空)

[root@localhost ~]# grep -n "." test.txt      
1:gd
2:god
4:good
5:goood
6:goad
8:gboad
[root@localhost ~]# grep -n "go.d" test.txt
4:good
6:goad

2.2、 :匹配其前字符任意次,如o,可以是没有o或者一个o,也可以是多个o

[root@localhost ~]# grep -n "*" test.txt
[root@localhost ~]# grep -n "o*" test.txt
1:gd
2:god
3:
4:good
5:goood
6:goad
7:
8:gboad
[root@localhost ~]# echo "gbad" >>test.txt
[root@localhost ~]# echo "pbad" >>test.txt
[root@localhost ~]# echo "kgbad" >>test.txt
[root@localhost ~]# echo "poad" >>test.txt  
[root@localhost ~]# grep -n "go*" test.txt 【o可以没有,o前面的g一定要匹配】
1:gd
2:god
4:good
5:goood
6:goad
8:gboad
9:gbad
11:kgbad

*2.3、. :匹配任意字符(匹配所有),可以为空**

[root@localhost ~]# grep -n ".*" test.txt
1:gd
2:god
3:
4:good
5:goood
6:goad
7:
8:gboad
9:gbad
10:pbad
11:kgbad
12:poad
[root@localhost ~]# grep -n "go.*" test.txt
2:god
4:good
5:goood
6:goad
[root@localhost ~]# grep -n "po.*" test.txt 
12:poad
[root@localhost ~]# echo "pgoad" >>test.txt   
[root@localhost ~]# grep -n "go.*" test.txt  【匹配go后存在任意字符,可为空】
2:god
4:good
5:goood
6:goad
13:pgoad
[root@localhost ~]#
[root@localhost ~]# grep -n "o.*" test.txt 
2:god
4:good
5:goood
6:goad
8:gboad
12:poad

2.4、^ :匹配紧接着后面的正则表达式,以...为开头

[root@localhost tmp]# grep "^root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost tmp]#

2.5、$ :匹配紧接着前面的正则表达式,以...结尾

[root@localhost tmp]# grep "bash$" /etc/passwd | head -1
root:x:0:0:root:/root:/bin/bash
[root@localhost tmp]#
^$:表示是空行的意思
“#|^$”:匹配以#号开头的注释行和空行

2.6、[] :匹配方括号里的任一字符

(如[sS],匹配s或匹配S),其中可用连字符(-)指定连字符的范围(如[(0-9)],匹配0-9任一字符);[^0-9]如果^符号出现在方括号的第一个位置,则表示匹配不在列表中的任一字符。

[root@localhost tmp]# cat hosts
192.168.200.1
192.168.200.3
a.b.123.5
23.c.56.1
1456.1.2.4
12.4.5.6.8
[root@localhost tmp]# grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' hosts  
192.168.200.1
192.168.200.3
1456.1.2.4
12.4.5.6.8
[root@localhost tmp]# grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3}$' hosts
192.168.200.1
192.168.200.3
[root@localhost tmp]#

2.7、? :匹配前面字符的零次或多次

[root@localhost ~]# grep -E "go?d" test.txt  
gd
god
[root@localhost ~]#
[root@localhost tmp]# cat test
do
does
doxy
[root@localhost tmp]# grep -E "do(es)?" test
do
does
doxy
[root@localhost tmp]#

3、POSIX BRE(基本正则)中才有的字符

{n,m} :区间表达式,匹配在它前面的单个字符重现【重复,紧接着的单个字符如https{0,1},即重复s 0-1次。{n}指匹配n次;{n,m}指匹配n至m次,{n,}指匹配至少n次,{,m}匹配至多m次。【\转义字符】

4、POSIX ERE(扩展正则)中才有的字符

4.1、{n,m} :与BRE的{n,m}功能相同

[root@localhost tmp]# grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3}$' hosts
192.168.200.1
192.168.200.3

4.2、+ :匹配前面正则表达式的一次或多次

[root@localhost ~]# egrep "go+d" test.txt
god
good
goood
[root@localhost ~]#

4.3、| :表示匹配多个字符串【或的关系】

[root@localhost ~]# grep -E "3306|1521" /etc/services
mysql           3306/tcp                        # MySQL
mysql           3306/udp                        # MySQL
ncube-lm        1521/tcp                # nCube License Manager
ncube-lm        1521/udp                # nCube License Manager
[root@localhost ~]#

4.4、( ) :分组过滤,后向引用

分组过滤   

[root@localhost ~]# echo "glad" >> test.txt
[root@localhost ~]# egrep "(la|oo)" test.txt
good
goood
glad

()后向引用;当前面匹配部分用小括号的时候,第一个括号的内容可以在后面部分用\1输出;以此类推。

 [root@localhost tmp]# ifconfig |sed -rn 's#.*addr:(.*)(B.*)$#\1#gp'
192.168.4.27 

5、正则表达式的元字符

5.1、\b :匹配一个单词边界

[root@localhost tmp]# cat test       
do
does
doxy
agdoeg
[root@localhost tmp]# grep "do\b" test
do
[root@localhost tmp]# grep "\bdo" test       
do
does
doxy
[root@localhost tmp]# grep "\bdoes" test         
does
[root@localhost tmp]# grep "\bdo\b" test 
do
[root@localhost tmp]#

5.2、\B :匹配非单词边界,与\b相反

[root@localhost tmp]# grep "do\B" test   
does
doxy
agdoeg
[root@localhost tmp]# grep "do\b" test
do
[root@localhost tmp]#

5.3、\d :匹配一个数字字符,等价于[0-9]

5.4、\D :匹配一个非数字字符,等价于[^0-9]

5.5、\w :匹配字母、数字、下划线,等价于[A-Za-z0-9_]

还有很多元字符,这里就不一一罗列出来

案例:开机精简

[root@localhost ~]# chkconfig --list| egrep -v "crond|network|rsyslog|sshd|sysstat" | awk '{print "chkconfig",$1,"off"}'|bash

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

JS字符串方法使用步骤详解

JS数组方法使用步骤详解

以上是Linux中正则表达式使用详解的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

centos和ubuntu的区别 centos和ubuntu的区别 Apr 14, 2025 pm 09:09 PM

CentOS 和 Ubuntu 的关键差异在于:起源(CentOS 源自 Red Hat,面向企业;Ubuntu 源自 Debian,面向个人)、包管理(CentOS 使用 yum,注重稳定;Ubuntu 使用 apt,更新频率高)、支持周期(CentOS 提供 10 年支持,Ubuntu 提供 5 年 LTS 支持)、社区支持(CentOS 侧重稳定,Ubuntu 提供广泛教程和文档)、用途(CentOS 偏向服务器,Ubuntu 适用于服务器和桌面),其他差异包括安装精简度(CentOS 精

Centos停止维护后的选择 Centos停止维护后的选择 Apr 14, 2025 pm 08:51 PM

CentOS 已停止维护,替代选择包括:1. Rocky Linux(兼容性最佳);2. AlmaLinux(与 CentOS 兼容);3. Ubuntu Server(需要配置);4. Red Hat Enterprise Linux(商业版,付费许可);5. Oracle Linux(与 CentOS 和 RHEL 兼容)。在迁移时,考虑因素有:兼容性、可用性、支持、成本和社区支持。

centos如何安装 centos如何安装 Apr 14, 2025 pm 09:03 PM

CentOS 安装步骤:下载 ISO 映像并刻录可引导媒体;启动并选择安装源;选择语言和键盘布局;配置网络;分区硬盘;设置系统时钟;创建 root 用户;选择软件包;开始安装;安装完成后重启并从硬盘启动。

docker desktop怎么用 docker desktop怎么用 Apr 15, 2025 am 11:45 AM

如何使用 Docker Desktop?Docker Desktop 是一款工具,用于在本地机器上运行 Docker 容器。其使用步骤包括:1. 安装 Docker Desktop;2. 启动 Docker Desktop;3. 创建 Docker 镜像(使用 Dockerfile);4. 构建 Docker 镜像(使用 docker build);5. 运行 Docker 容器(使用 docker run)。

docker原理详解 docker原理详解 Apr 14, 2025 pm 11:57 PM

Docker利用Linux内核特性,提供高效、隔离的应用运行环境。其工作原理如下:1. 镜像作为只读模板,包含运行应用所需的一切;2. 联合文件系统(UnionFS)层叠多个文件系统,只存储差异部分,节省空间并加快速度;3. 守护进程管理镜像和容器,客户端用于交互;4. Namespaces和cgroups实现容器隔离和资源限制;5. 多种网络模式支持容器互联。理解这些核心概念,才能更好地利用Docker。

vscode需要什么电脑配置 vscode需要什么电脑配置 Apr 15, 2025 pm 09:48 PM

VS Code 系统要求:操作系统:Windows 10 及以上、macOS 10.12 及以上、Linux 发行版处理器:最低 1.6 GHz,推荐 2.0 GHz 及以上内存:最低 512 MB,推荐 4 GB 及以上存储空间:最低 250 MB,推荐 1 GB 及以上其他要求:稳定网络连接,Xorg/Wayland(Linux)

怎么看docker进程 怎么看docker进程 Apr 15, 2025 am 11:48 AM

Docker 进程查看方法:1. Docker CLI 命令:docker ps;2. Systemd CLI 命令:systemctl status docker;3. Docker Compose CLI 命令:docker-compose ps;4. Process Explorer(Windows);5. /proc 目录(Linux)。

docker镜像失败怎么办 docker镜像失败怎么办 Apr 15, 2025 am 11:21 AM

Docker镜像构建失败的故障排除步骤:检查Dockerfile语法和依赖项版本。检查构建上下文中是否包含所需源代码和依赖项。查看构建日志以获取错误详细信息。使用--target选项构建分层阶段以识别失败点。确保使用最新版本的Docker引擎。使用--t [image-name]:debug模式构建镜像以调试问题。检查磁盘空间并确保足够。禁用SELinux以防止干扰构建过程。向社区平台寻求帮助,提供Dockerfile和构建日志描述以获得更具体的建议。

See all articles