通配符和正则表达式的区别

CMake中, 字符串都有通配符和正则表达式的支持, 但二者是有区别的, 所以使用的时候不要混淆。
通配符表达式: Globbing  Expressions
正则表达式: Regular Expression

以下为转载内容 (以linux为例):
在linux中,有通配符及正则表达式,那么什么是通配符和正则表达式,什么时候用?
 参考 https://www.cnblogs.com/zejin2008/p/5990863.html

通配符

它是由shell解析,并且一般用于 匹配文件名,实际上就是shell解释器去解析的特殊符号,linux系统通配符有几下几种:
*:匹配任意多个字符
?:匹配任意一个字符
[...]:匹配中括号内出现的任意一个字符
[!...]:不匹配中括号内出现的任意一个字符
 
一般出现在要shell命令或脚本中,匹配特定的文件名
 
 

正则表达式

它是一个字符匹配标准,一些命令工具按此标准实现 字符匹配 ,根据命令支持的匹配功能可分为基础正则表达式和扩展正则表达式,常用于支持正则表达式的工具,如sed,grep,awk...
有如下一些常用的匹配元字符:
 
字符匹配
.:匹配任意单个字符
*:匹配其前面一个字符出现任意次
?:匹配其前面的字符1次或0次
+:匹配其前面一个字符出现至少一次(在扩展正则表达式中)
 
位置匹配
^:锚定行首
$:锚定行尾
\<或\b:锚定词首,其后面的任意字符必须作为单词首部出现
\>或\b:锚定词尾,其前面的任意字符必须作为单词尾部出现
\B:非单词的开头或结尾
^$:空白行
 
分组() :
(ab)* : 匹配ab这个分组出现任意次
\1:引用第一个左括号以及与之对应的右括号所包括的所有内容,同理还有\2,\3
 
特殊子字符类:
[:alnum:] :任何字母和数字
[:alpha:] :任何字母
[:cntrl:] :控制字符. 在ASCII表中对应八进制000 到 037, 和177 ('DEL').
[:digit:] :任何数字
[:graph:] :匹配打印字符,相当于'[:alnum:]' +'[:punct:]'.
[:lower:] :小写字母
[:print:] :可打印字符,相当于 '[:alnum:]', '[:punct:]', 和space.
[:punct:] 标点符号,'! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ' { | } ~'
[:space:] 空白字符,tab, newline, vertical tab, form feed, carriage return, and space.
[:upper:] 大写字母
[:xdigit:] 任何16进制的数字,相当于[0-9a-fA-F]
所以:^[[:space:]]*$ :匹配没有任何内容的行
 
需要注意:[]中的特殊字符如.已经失去了其特殊含义,和方括号中的普通字符[abc]一样。

对于CMake的这则表达式的定义, CMake官网对其做了明确说明:
https://cmake.org/cmake/help/latest/command/string.html?highlight=regular%20expression#regex-specification

Regex Specification

The following characters have special meaning in regular expressions:

^
Matches at beginning of input
$
Matches at end of input
.
Matches any single character
[ ]
Matches any character(s) inside the brackets
[^ ]
Matches any character(s) not inside the brackets
-
Inside brackets, specifies an inclusive range between characters on either side e.g.  [a-f] is  [abcdef] To match a literal  - using brackets, make it the first or the last character e.g.  [+*/-] matches basic mathematical operators.
*
Matches preceding pattern zero or more times
+
Matches preceding pattern one or more times
?
Matches preceding pattern zero or once only
|
Matches a pattern on either side of the  |
()
Saves a matched sub expression, which can be referenced in the  REGEX REPLACEoperation. Additionally it is saved by all  regular  expression-related commands, including e.g.  if(MATCHES), in the variables  CMAKE_MATCH_<n> for  <n> 0..9.

*+ and ? have higher precedence than concatenation. | has lower precedence than concatenation. This means that the regular expression ^ab+d$ matches abbd but not ababd, and the regular expression ^(ab|cd)$ matches ab but not abd.



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值