当前位置: 首页 > news >正文

正则表达式八:子表达式匹配

正则表达式八:子表达式匹配

介绍

用来提供成组匹配

元字符列表

  • 成组匹配:()
  • 嵌套成组匹配:(()|())

成组匹配(()

用于将字符内容看作一个整体

示例

原始内容:

Hello, my name is Ben Forta, and I am
the author of multiple books on SQL (including
MySQL, Oracle PL/SQL, and SQL Server T-SQL),
Regular&nbs;&nbs;Expressions, and other subjects.

正则表达式1:

&nbs;{2,}

Hello, my name is Ben Forta, and I am
the author of multiple books on SQL (including
MySQL, Oracle PL/SQL, and SQL Server T-SQL),
Regular&nbs;&nbs;Expressions, and other subjects.

说明:无内容匹配上
;{2,}代表符号;至少匹配两次

正则表达式2:

(&nbs;){2,}

Hello, my name is Ben Forta, and I am
the author of multiple books on SQL (including
MySQL, Oracle PL/SQL, and SQL Server T-SQL),
Regular&nbs;&nbs;Expressions, and other subjects.

说明:(&nbs;)代表将字符&nbs;成组,当成一个整体
{2,}代表至少匹配两次成组内容

嵌套成组匹配((()|())

支持成组嵌套

示例

原始内容:

Pinging hog.forta.com [12.159.46.200]
with 32 bytes of data:

正则表达式:

(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))\.){3}((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))

Pinging hog.forta.com [12.159.46.200]
with 32 bytes of data:

说明:|表示或者的意思
(25[0-5])代表匹配250-255
(2[0-4]\d)代表匹配200-249
(1\d{2})代表匹配100-199
(\d{1,2})代表匹配0-99
((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))表示从左至右依次进行匹配,匹配成功即停止,且只匹配一次