compile(string, option = nil, code = nil) -> Regexp[permalink][rdoc][edit]new(string, option = nil, code = nil) -> Regexp-
文字列
stringをコンパイルして正規表現オブジェクトを生成して返します。第一引数が正規表現オブジェクトであればそれを複製して返します。その場合、第二、第三引数は警告の上無視されます。
- [PARAM]
string: - 正規表現を文字列として与えます。
- [PARAM]
option: - Regexp::IGNORECASE, Regexp::MULTILINE, Regexp::EXTENDED の論理和を指定します。 Integer 以外であれば真偽値の指定とみなされ、真なら Regexp::IGNORECASE の指定と同じになります。
- [PARAM]
code: -
"n"または"N"を与えると、パターンを ASCII-8BIT(バイナリ)のパターンとして扱います。パターンが ASCII 文字だけの場合、返り値のエンコーディングは US-ASCII になります。それ以外の指定は無視されます。 - [EXCEPTION]
RegexpError: - 正規表現のコンパイルに失敗した場合発生します。
str = "This is Regexp" t1 = Regexp.compile("this is regexp", Regexp::IGNORECASE) t1.match(str) p $~ # => "This is Regexp" t2 = Regexp.compile(' this # ここは使用されない \ is \ regexp # ここも使用されない ', Regexp::EXTENDED | Regexp::IGNORECASE) t2.match(str) p Regexp.last_match # => "This is Regexp" str = "ふるいけや\nかわずとびこむ\nみずのおと" t2 = Regexp.compile("ふる.*?と", Regexp::MULTILINE) p t2.match(str)[0] # => "ふるいけや\nかわずと" - [PARAM]