Grammar-based construction 语法驱动的构造

有一类应用,从外部读取文本数据, 在应用中做进一步处理:

适用场景:

– A file on disk, in which case the specification is called the file format 输入 文件有特定格式,程序需读取文件并从中抽取正确的内容

– Messages sent over a network, in which case the specification is a wire protocol 从网络上传输过来的消息,遵循特定的协议

– A command typed by the user on the console, in which case the specification is a command line interface 用户在命令行输入的指令,遵循 特定的格式

– A string stored in memory 内存中存储的字符串,也有格式需要

三种基本的正则操作:

 

语法

常用的元字符
代码说明
.匹配除换行符以外的任意字符
\w匹配字母或数字或下划线或汉字
\s匹配任意的空白符
\d匹配数字
\b匹配单词的开始或结束
^匹配字符串的开始(在集合字符里[^a]表示非(不匹配)的意思
$匹配字符串的结束
常用的限定符
代码/语法说明
*重复零次或更多次
+重复一次或更多次
?重复零次或一次
{n}重复n次
{n,}重复n次或更多次
{n,m}重复n到m次

▪ . any single character

▪ \d any digit, same as [0-9]

▪ \s any whitespace character, including space, tab, newline

▪ \w any word character, including underscore, same as [a-zA-Z_0-9]

▪ \., \(, \), \*, \+, ... escapes an operator or special character so that it matches literally 

常用的反义字符
代码/语法说明
\W匹配任意不是字母,数字,下划线,汉字的字符
\S匹配任意不是空白符的字符
\D匹配任意非数字的字符
\B匹配不是单词开头或结束的位置
[^x]匹配除了x以外的任意字符

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值