| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「Excel VBA 正規表現を使う」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
(ページの作成:「==Excel VBA 正規表現を使う== [Excel VBA]{{category 正規表現}}{{category VBAソース片}} [http://msdn.microsoft.com/library/ja/default.asp?url=/library/ja…」)
 
 
(同じ利用者による、間の2版が非表示)
1行目: 1行目:
==Excel VBA 正規表現を使う==
+
==[[Excel VBA 正規表現を使う]]==
[Excel VBA]{{category 正規表現}}{{category VBAソース片}}
+
[[Excel VBA]] | [[Category:正規表現}}{{category VBAソース片]]
  
 
[http://msdn.microsoft.com/library/ja/default.asp?url=/library/ja/script56/html/vsobjRegExp.asp MSDN Visual Basic Scripting Edition RegExp オブジェクト]
 
[http://msdn.microsoft.com/library/ja/default.asp?url=/library/ja/script56/html/vsobjRegExp.asp MSDN Visual Basic Scripting Edition RegExp オブジェクト]
10行目: 10行目:
 
  Dim value  As String '解析する文字列
 
  Dim value  As String '解析する文字列
 
   
 
   
  Set reg = CreateObject("VBScript.RegExp")
+
  Set reg = CreateObject("[[VBScript]].RegExp")
 
  reg.Pattern = "^[ ]+([0-9A-Z@]+).*[ ]FOUND[ ].*"
 
  reg.Pattern = "^[ ]+([0-9A-Z@]+).*[ ]FOUND[ ].*"
 
  'reg.Global = True '/g オプションを指定する
 
  'reg.Global = True '/g オプションを指定する
 
  Set matches = reg.Execute(value)
 
  Set matches = reg.Execute(value)
 
   
 
   
  If matches.Count > 0 Then
+
  If matches.Count > 0 Then
 
     ' 一致情報のコレクション
 
     ' 一致情報のコレクション
 
     For Each match In matches   
 
     For Each match In matches   
35行目: 35行目:
 
   
 
   
 
  i = 0
 
  i = 0
  ReDim indexes(i)
+
  [[R]]eDim indexes(i)
 
   
 
   
  Set reg = CreateObject("VBScript.RegExp")
+
  Set reg = CreateObject("[[VBScript]].RegExp")
 
  reg.Pattern = "([0-9]{1,3})"
 
  reg.Pattern = "([0-9]{1,3})"
 
  reg.Global  = True
 
  reg.Global  = True
 
   
 
   
 
  Set matches = reg.Execute(value)
 
  Set matches = reg.Execute(value)
  If matches.Count > 0 Then
+
  If matches.Count > 0 Then
 
     ' 一致情報のコレクション
 
     ' 一致情報のコレクション
 
     For Each match In matches
 
     For Each match In matches
         ReDim Preserve indexes(i)
+
         [[R]]eDim Preserve indexes(i)
 
         indexes(i) = match.SubMatches(0)
 
         indexes(i) = match.SubMatches(0)
 
         i = i + 1
 
         i = i + 1
 
     Next
 
     Next
 
  End If
 
  End If

2020年2月16日 (日) 04:25時点における最新版

Excel VBA 正規表現を使う

Excel VBA | [[Category:正規表現}}{{category VBAソース片]]

MSDN Visual Basic Scripting Edition RegExp オブジェクト

基本的な使い方

Dim reg     As Object
Dim matches As Object
Dim match   As Object
Dim value   As String '解析する文字列

Set reg = CreateObject("VBScript.RegExp")
reg.Pattern = "^[ ]+([0-9A-Z@]+).*[ ]FOUND[ ].*"
'reg.Global = True '/g オプションを指定する
Set matches = reg.Execute(value)

If matches.Count > 0 Then
    ' 一致情報のコレクション
    For Each match In matches   
        Debug.Print match.FirstIndex
        Debug.Print match.Value
    Next
    
    Set match = matches(0)
    ' 一致グループ
    Debug.Print match.SubMatches(0)
End If

"(...)"に一致する箇所を抜き出して配列に格納

Dim reg       As Object
Dim matches   As Object
Dim match     As Object
Dim indexes() As String
Dim i         As Integer

i = 0
ReDim indexes(i)

Set reg = CreateObject("VBScript.RegExp")
reg.Pattern = "([0-9]{1,3})"
reg.Global  = True

Set matches = reg.Execute(value)
If matches.Count > 0 Then
    ' 一致情報のコレクション
    For Each match In matches
        ReDim Preserve indexes(i)
        indexes(i) = match.SubMatches(0)
        i = i + 1
    Next
End If