Skip to content

Instantly share code, notes, and snippets.

@tamago324
Created May 31, 2018 01:02
Show Gist options
  • Save tamago324/621f42fcba6cb195f8c7f541828689ef to your computer and use it in GitHub Desktop.
Save tamago324/621f42fcba6cb195f8c7f541828689ef to your computer and use it in GitHub Desktop.
"(aa) and (bb) and (cc)" から"(aa)"、"(bb)"、"(cc)"を取得する正規表現
Sub test()
Dim regObj As Object
Set regObj = CreateObject("VBScript.RegExp")
Dim matchs As Object
Dim match As Object
Dim sTest As String
sTest = "(AA) and (BB) and (CC)"
' [^\)]* は")"ではない文字が0以上 という意味
regObj.Pattern = "\([^\)]*\)"
regObj.Global = True
Set matchs = regObj.Execute(sTest)
For Each match In matchs
Debug.Print (match)
Next
End Sub
(AA)
(BB)
(CC)

"\([^\)]*\)"(.*)のそれぞれを取得できる。[^\)]*")"ではない文字が0以上という意味になるため

もし、"\(.*\)"とした場合、一つずつに分けることはできない

regObj.Pattern = "\(.*\)"

実行結果

(AA) and (BB) and (CC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment