0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

vbaコード置き場20250922

Posted at

'' **************************************************************
''機能名 :CaseInsensitiveLike
''返り値 : Boolean
''引数 :
'' s As String … 対象
'' pat As String … パターン(* のみワイルドカード)
''機能説明 : 大小無視・空白/改行正規化のうえ、?/#/[] はリテラル化、* はワイルドカードのまま
'' **************************************************************
Private Function CaseInsensitiveLike(ByVal s As String, ByVal pat As String) As Boolean
Dim a As String, b As String
a = NormalizeForCompare(s)
b = NormalizeForCompare(pat)
b = EscapeLike_AsteriskOnly(b)
CaseInsensitiveLike = (a Like b)
End Function

'' **************************************************************
''機能名 :EscapeLike_AsteriskOnly
''返り値 : String
''引数 :
'' p As String … パターン
''機能説明 : Like用に ? / # / [ / ] は常にリテラル化、* はワイルドカードとして維持
'' 「*」と書けばリテラルの * を表現できる(内部で [*] に変換)
'' **************************************************************
Private Function EscapeLike_AsteriskOnly(ByVal p As String) As String
Dim t As String: t = p
' リテラル * 指定(*)を一時退避(私用領域をプレースホルダに使用)
t = Replace$(t, "*", ChrW$(&HE000))

' Like のメタをリテラル化(* は残す)
t = Replace$(t, "[", "[[]")
t = Replace$(t, "]", "[]]")
t = Replace$(t, "#", "[#]")
t = Replace$(t, "?", "[?]")

' リテラル * を復元([*] は「* のみ」を表す文字クラス)
t = Replace$(t, ChrW$(&HE000), "[*]")

EscapeLike_AsteriskOnly = t

End Function

Private Function NormalizeForCompare(ByVal s As String) As String
Dim t As String: t = NzStr(s)
t = Replace$(t, Chr$(160), " ") 'NBSP
t = Replace$(t, ChrW$(&H3000), " ") '全角スペース
t = Replace$(t, vbCr, "")
t = Replace$(t, vbLf, "")
t = Replace$(t, vbTab, " ")
t = Replace$(t, ChrW$(&H200B), "") 'ZWSP
NormalizeForCompare = UCase$(Trim$(t))
End Function

0
1
0

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?