What have you found for these years?

2009-11-23

ruby regexp notes

有些特殊的東西,還真是有點難查... 總算找到一個地方有寫:
Ruby Regular Expressions
====
updated:
噢,不過剛剛才找到,這個才真正完整:
Regular Expressions Overview
這樣我應該不用再整理一次了....
====

節錄重點於此:

regexp modifiers: (suffix of regexp literal, e.g. /a/i)
i => case insensitive
m => multi-line, e.g. (.) would match \n
x => ignore whitespace in regexp, e.g. /a b/ == /ab/
u => unicode regexp, 1.8 only i guess, 1.9 is encoding sensitive

group options: (perhaps 1.9 only)
(?i) => toggle on i for rest regexp (so do m, x and so on)
(?-i) => toggle off i for rest regexp

for example:

'aAa' =~ /a(?i)a(?-i)a/

(?:blah) => group without back-reference
that is, it won't be counted in $1, $2, so on so forth.

(?i:blah) => toggle on i in group, and no back-reference too
(?-i) => toggle off i in group, and no back-reference too

(?#blah) => comments embedded in regexp...
(?=blah) => look ahead match (peek)
(?!blah) => look ahead not match
(?<=blah) => look behind match
(?<!blah) => look behind not match

然後是上面那網站上沒列的: named group

(?blah) => you can refer this group in regexp by:
\g. that is, you don't have to use \1, \2, etc.

non-greedy 應該每個人都知道吧? *?

\b => match word boundary (e.g. whitespace)
\B => match non-(word boundary)
\A => beginning of string
\Z => end of string (ignore last newline)
\z => end of string (include last newline)
\G => 還沒完全搞懂....

不過剛剛才發現這網頁寫得很完整,那我就不整理了
Regular Expressions Overview

0 retries:

Post a Comment

Note: Only a member of this blog may post a comment.



All texts are licensed under CC Attribution 3.0