VBScript.RegExp版正規表現クラス
Regex11はかっこによる後方参照等が使えないので別の正規表現クラスを作ってみた
使用例
'aba' =~ '(.).\1' => true 'abc' replace: '(.)(.*)' to: '$2$1' => 'bca' ('abcdef' rmatch: '(..)(.)') at: 2 at: 1 => 'de'
Regexpクラス
インスタンス変数reを持つRegexpクラスを定義する。
reはVBScript.RegExpオブジェクトの格納に使う。
以下のクラスメソッドを定義。
new: pat ^super new initialize: pat
以下のインスタンスメソッドを定義。
initialize: pat re := COMDispatchDriver createObject: 'VBScript.RegExp'. re setProperty: 'pattern' value: pat. re setProperty: 'global' value: true. ^self
test: str ^re invokeMethod: 'test' with: str
replace: src to: dst ^re invokeMethod: 'replace' withArguments: (Array with: src with: dst)
execute: str ^Matches new: (re invokeMethod: 'execute' with: str)
Matchesクラス
インスタンス変数matchを持つMatchesクラスを定義する。
matchはexecute:メソッドが返すMatchesコレクションオブジェクトの格納に使う。
(という説明でinitialize:とnew:のコードは省略)
size ^match getProperty: 'count'
at: n ^(match getProperty: 'item' with: n - 1) getProperty: 'value'
sizeat: n ^((match getProperty: 'item' with: n - 1) getProperty: 'submatches') getProperty: 'count'
at: m at: n ^((match getProperty: 'item' with: m - 1) getProperty: 'submatches') getProperty: 'item' with: n - 1
CharacterArrayクラス
既存のCharacterArrayクラスに以下のメソッドを追加。
本当はmatch:にしたかったんだけどすでにあったのでrmatch:にした。
=~ pat ^(Regexp new: pat) test: self
replace: pat to: dst ^(Regexp new: pat) replace: self to: dst
rmatch: pat ^(Regexp new: pat) execute: self
問題点
呼び出しをするたびに以下のWarningが出る…
COM Connect Development Warning COM interface released by finalization: an IDispatchPointer {0161A070}
The COMDispatchDriver release message should always be called when you are done with an object.(page130/340)
とマニュアルに書いてあるので、RegexpオブジェクトがGCされるときにre releaseを実行するフックを入れればいいのだろうか…