正则表达式介绍


正则表达式介绍

正则表达式是一些用来匹配和处理文本的字符串。 正则表达式是用正则表达式语言创建的;正则表达式语言并不是一种完备的程序设计语言,而是内置于其他语言或软件产品里的“迷你”语言。、

匹配单个字符

匹配纯文本

文本:"https://www.supremepole.com" will be a great website.

正则表达式:website

结果:"https://www.supremepole.com" will be a great website.

匹配任意字符

可以使用“.”来表示匹配任意字符。

文本:enable, variable, capable, extensive, intensive

正则表达式:.able

结果:enable, variable, capable, extensive, intensive

匹配特殊字符

我们可以使用“.”来表示匹配任意字符,那么如果想匹配文本中“.”本身,那么需要在“.”前面加上“\”符号。

文本:file.word, file.xlsx, file.pptx

正则表达式:file.

结果:file.word, file.xlsx, file.pptx

匹配一组字符

字符集合

我们可以使用“[]”来定义待匹配的字符集合。

文本:Word is the same as word if it is not case insensitive.

正则表达式:[Ww]ord

结果:Word is the same as word if it is not case insensitive.

字符区间

我们可以使用“-”来定义字符区间。比如常见的字符区间有“[0-9]”。

文本:file.word, file1.xlsx, file2.pptx

正则表达式:file[0-9]

结果:file.word, file1.xlsx, file2.pptx

取非匹配

文本:file.word, file1.xlsx, file2.pptx

正则表达式:file[^0-9]

结果:file.word, file1.xlsx, file2.pptx

上次编辑于:
贡献者: Neil