控制字符

来源:互联网 发布:淘宝加入购物车不弹 编辑:程序博客网 时间:2024/06/08 23:53

-- Start

ASCII 编码共定义了128个字符,其中前32位都是一些控制字符。我们已经知道了如何用 \n 来匹配换行符,除此之外,我们还可以使用 \cJ。

#!/usr/bin/perlmy $testText = "I lovereg\nular expressions.";if($testText =~ m/reg\cJular/) {print "finds the word.";} else {print "cannot find the word.";}
public static void main(String[] args) {String testText = "I love reg\nular expressions.";String regExp = "reg\\cJular";Pattern p = Pattern.compile(regExp);Matcher m = p.matcher(testText);if (m.find()) {System.out.println("finds the word.");} else {System.out.println("cannot find the word.");}}

如上所见,我们可以通过使用 \c后加一个大写字母的方式来匹配这些控制字符。但是,这种做法并不推荐,原因是好多工具并不支持这种写法,如果你要匹配控制字符,尽量使用八进制或十六进制转义吧。

--更多参见:正则表达式精萃
-- 声 明:转载请注明出处
-- Last Updated on 2012-05-07
-- Written by ShangBo on 2012-05-07
-- End