LoginSignup
0
2

More than 5 years have passed since last update.

VBScriptにないLikeを作るVBLike

Last updated at Posted at 2016-12-18

VBAでも使えます。
ワイルドカードも使えます

書式

vblike("検索したい文字列,"検索する文字")
でヒットすればtrue , しなければ fales が返ります
chr(13)とchr(10)は自信がありません

注意点

VBAとかにコピーすると右斜め下に行く斜線は円マークになります。

カスタマイズ

自動的にエスケープする機能は状況によって書き換えが効きます
たとえば
http://www.excel.studio-kazu.jp/kw/20100316224856.html
このようなファイル名と拡張子が付く検索であれば
ピリオドの行(8行目)の’を外すと可能になります。
また正規表現用の[A-z]などをあらかじめ宣言しているので
必要に応じ & 結合で加えてカスタマイズしやすくしました。

気付いたこと

円マークのエスケープはやるなら冒頭でないといけません。そうしないと円が増えてしまうためです。
あと垂直タブって何だろう。これはエスケープしていません。

vblike.Vbs
Function VbLike(sTest, sPattern)
Dim objRE,sAdd1,sAdd_1,xAdd,UAdd,AAdd
Set objRE = CreateObject("VBScript.RegExp")
'escape character add
'spattern = Replace(sPattern, "\", "\\", 1, -1)
'spattern = Replace(sPattern, "*", "\*", 1, -1) 'wildcard escape
'spattern = Replace(sPattern, "?", "\?", 1, -1) 'wildcard escape
'sPattern = Replace(sPattern, ".", "\.", 1, -1) 'wildcard escape
spattern = Replace(spattern, "+", "\+", 1, -1)
spattern = Replace(spattern, "|", "\|", 1, -1)
spattern = Replace(spattern, ")", "\)", 1, -1)
spattern = Replace(spattern, "(", "\(", 1, -1)
spattern = Replace(spattern, "[", "\[", 1, -1)
spattern = Replace(spattern, "]", "\]", 1, -1)
spattern = Replace(spattern, Chr(13), "\r", 1, -1)
spattern = Replace(spattern, Chr(10), "\n", 1, -1)
spattern = Replace(spattern, vbTab, "\t", 1, -1)
spattern = Replace(spattern, " ", "\s", 1, -1)
spattern = Replace(spattern, " ", "\s", 1, -1)
'Add character
sAdd1="\w" '[a-zA-Z_0-9]
sAdd_1="\W" '[^a-zA-Z_0-9] 
xAdd = "\50" '( 50 is 8 digit ASCII
uAdd = "\u00A3" '( 00A3 is 16 digit UNICODE
AAdd="[A-z]"
objRE.Pattern = "^.*" & spattern & ".*$"
If objRE.test(sTest) = True Then
 VbLike = True
Else
 VbLike = False
End If
Set objRE = Nothing
End Function

参考文献

VBScript(VBスクリプト)でLikeするには?
正規表現による Visual Basic Scripting Edition (VBScript) の機能強化

0
2
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2