ruby学习笔记系列(5)

Reguler Expression
\d [0-9] Digit character
\D [^0-9] Any character except a digit
\s [\s\t\r\n\f] Whitespace character
\S [^\s\t\r\n\f] Any character except whitespace
\w [A-Za-z0-9_] Word character
\W [^A-Za-z0-9_] Any character except a word character

[:alnum:] Alphanumeric
[:alpha:] Uppercase or lowercase letter
[:blank:] Blank and tab
[:cntrl:] Control characters (at least 0x00–0x1f, 0x7f)
[:digit:] Digit
[:graph:] Printable character excluding space
[:lower:] Lowercase letter
[:print:] Any printable character (including space)
[:punct:] Printable character excluding space and alphanumeric
[:space:] Whitespace (same as \s)
[:upper:] Uppercase letter
[:xdigit:] Hex digit (0–9, a–f, A–F)

r* matches zero or more occurrences of r.
r+ matches one or more occurrences of r.
r? matches zero or one occurrence of r.
r{m,n} matches at least “m” and at most “n” occurrences
r{m,} matches at least “m” occurrences of r.
r{m} matches exactly “m” occurrences of r.

Backslash Sequences in the Substitution
Earlier we noted that the sequences \1, \2, and so on, are available in the pattern,
standing for the nth group matched so far. The same sequences are available in the
second argument of sub and gsub.
"fred:smith".sub(/(\w+):(\w+)/, '\2, \1') ! "smith, fred"
"nercpyitno".gsub(/(.)(.)/, '\2\1') ! "encryption"
Additional backslash sequences work in substitution strings: \& (last match), \+ (last
matched group), \` (string prior to match), \' (string after match), and \\ (a literal
backslash).
It gets confusing if you want to include a literal backslash in a substitution. The obvious
thing is to write
str.gsub(/\\/, '\\\\')
Clearly, this code is trying to replace each backslash in str with two. The programmer
doubled up the backslashes in the replacement text, knowing that they’d be converted
to \\ in syntax analysis. However, when the substitution occurs, the regular expression
engine performs another pass through the string, converting \\ to \, so the net effect
is to replace each single backslash with another single backslash. You need to write
gsub(/\\/, '\\\\\\\\')!
str = 'a\b\c' ! "a\b\c"
str.gsub(/\\/, '\\\\\\\\') ! "a\\b\\c"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值