正则表达式(regular-expression)是一种模式匹配工具,用户根据一定的规则构建匹配模式,然后将匹配模式与目标对象进行匹配。正则表达式格式上是由1 256个普通字符和特殊字符组成的字符串。
表1 特殊字符及其语法意义描述 | ||
特殊字符 | 功能 | 举例 |
转义字符。将下一个字符(特殊字符或者普通字符)标记为普通字符。 | *匹配* | |
^ | 匹配行首的位置。 | ^10匹配10.10.10.1,不匹配20.10.10.1 |
$ | 匹配行尾的位置。 | 1$匹配10.10.10.1,不匹配10.10.10.2 |
* | 匹配前面的子正则表达式零次或多次。 | 10*可以匹配1、10、100、1000、…… (10)*可以匹配空、10、1010、101010、…… |
+ | 匹配前面的子正则表达式一次或多次。 | 10+可以匹配10、100、1000、…… (10)+可以匹配10、1010、101010、…… |
? | 匹配前面的子正则表达式零次或一次。 说明: 当前,在华为公司数据通信设备上运用正则表达式输入?时,系统显示为命令行帮助功能。华为公司数据通信设备不支持正则表达式输入?特殊字符。 | 10?可以匹配1或者10 (10)?可以匹配空或者10 |
. | 匹配任意单个字符。 | 0.0可以匹配0x0、020、…… .oo.可以匹配book、look、tool、…… |
() | 一对圆括号内的正则表达式作为一个子正则表达式,匹配子表达式并获取这一匹配。圆括号内也可以为空。 | 100(200)+可以匹配100200、100200200、…… |
x|y | 匹配x或y。 | 100|200匹配100或者200 1(2|3)4匹配124或者134,而不匹配1234、14、1224、1334 |
[xyz] | 匹配正则表达式中包含的任意一个字符。 | [123]匹配255中的2 |
[^xyz] | 匹配正则表达式中未包含的字符。 | [^123]匹配除123之外的任何字符 |
[a-z] | 匹配正则表达式指定范围内的任意字符。 | [0-9]匹配0到9之间的所有数字 |
[^a-z] | 匹配正则表达式指定范围外的任意字符。 | [^0-9]匹配所有非数字字符 |
最简单的正则表达式不包含任何特殊字符,例如,可以定义一个正则表达式“hello”,它只匹配字符串“hello”。实际应用中,是由多个普通字符和特殊字符组合使用,匹配某些特征的字符串。
除非特别说明,特殊字符的退化也适用于括号“()”内包含的子正则表达式。
使用正则表达式过滤命令行显示信息的方法有两种:
在命令中指定过滤方式
在支持正则表达式的命令中,有三种过滤方式可供选择:
系统对命令的显示信息进行过滤后,还支持上下文显示规则。上下文显示规则有如下几种:
下面举例来说明在命令中指定过滤方式的用法。
例1:执行命令display interface brief,显示不匹配“Ethernet”、“NULL”或“Tunnel”的所有行。
display interface brief | exclude Ethernet|NULL|Tunnel
PHY: Physical
*down: administratively down
^down: standby
(l): loopback
(s): spoofing
(b): BFD down
(e): ETHOAM down
(dl): DLDP down
(d): Dampening Suppressed
InUti/OutUti: input utility/output utility
Interface PHY Protocol InUti OutUti inErrors outErrors
Eth-Trunk1 down down 0% 0% 0 0
Eth-Trunk17 down down 0% 0% 0 0
LoopBack1 up up(s) 0% 0% 0 0
Vlanif1 up down -- -- 0 0
MEth0/0/1 down down 0% 0% 0 0
Vlanif2 down down -- -- 0 0
Vlanif10 down down -- -- 0 0
Vlanif12 down down -- -- 0 0
Vlanif13 down down -- -- 0 0
Vlanif20 up up -- -- 0 0
Vlanif22 down down -- -- 0 0
Vlanif222 down down -- -- 0 0
Vlanif4094 down down -- -- 0 0
例2:执行命令display current-configuration,只显示匹配正则表达式“vlan”的所有行。
display current-configuration | include vlan
vlan batch 2 10 101 to 102 800 1000
vlan 2
vlan 10
port trunk pvid vlan 800
undo port trunk allow-pass vlan 1
port trunk allow-pass vlan 10 101 800
undo port hybrid vlan 1
undo port hybrid vlan 1
port hybrid untagged vlan 10
undo port hybrid vlan 1
undo port hybrid vlan 1
以上举例的显示信息仅为示意,请以设备实际显示为准。
在分屏显示时指定过滤方式
支持在分屏显示时指定过滤方式的命令行有:
采用分屏显示时,可以在分屏提示符“---- More ----”中指定过滤类型:
例如:执行命令display current-configuration,当分屏显示时,在余下的回显中仅显示Vlanif相关的信息。
display current-configuration
!Software Version V200R009C00
#
sysname HUAWEI
#
vlan batch 10 to 11 100
#
hotkey CTRL_G "display tcp status"
#
lldp enable
#
undo http server enable
undo http secure-server enable
#
dhcp enable
#
dhcp snooping enable
+Vlanif //输入过滤方式
Filtering...
interface Vlanif10
interface Vlanif100
页面更新:2024-04-24
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号