ありがとう:
方法を1
Option Explicit
Dim strFilePath, ofso, ofile, strFileData, strKey, strPrev, strCurr
strFilePath="" '<-- Enter the absolute path of your .ini file in this variable
Set ofso = CreateObject("scripting.FileSystemObject")
Set ofile = ofso.OpenTextFile(strFilePath,1,False)
strKey = "Eastern North America" '<-- Enter Unique title for which you want the Area code
strPrev=""
strCurr=""
Do
strCurr = ofile.ReadLine
If InStr(1,strCurr,strKey)<>0 Then
Exit Do
End If
strPrev = strCurr
Loop Until ofile.AtEndOfStream
MsgBox strPrev
Set ofile = Nothing
Set ofso = Nothing
METHOD 2(使用した正規表現)
Option Explicit
Dim strFilePath, ofso, ofile, strFileData, strKey, re, objMatches
strFilePath="" '<-- Enter the absolute path of your .ini file in this variable
Set ofso = CreateObject("scripting.FileSystemObject")
Set ofile = ofso.OpenTextFile(strFilePath,1,False)
strFileData = ofile.ReadAll()
ofile.Close
strKey = "Eastern North America" '<-- Enter Unique title for which you want the Area code
Set re = New RegExp
re.Global=True
re.Pattern="\[([^]]+)]\s*Title="&strKey
Set objMatches = re.Execute(strFileData)
If objMatches.Count>0 Then
MsgBox objMatches.Item(0).Submatches.Item(0)
End If
Set re = Nothing
Set ofile = Nothing
Set ofso = Nothing
>>>Click here for Regex Demo<<<
正規表現の説明:
\[
から[
([^]]+)
リテラルマッチ - グループ
]
に]
ない任意の文字の1+発生をキャプチャ - マッチリテラル]
\s*
- ma 0個以上の空白文字(改行文字を含む)
Title=
- テキストTitle=
と一致します。これは、一意のタイトルの値を含む変数strKey
と連結されます。
ありがとうGurmanさん、ありがとうございます。でも、私はあなたのスクリプトをスクリプト化の代わりにADODB.Streamで使うことができます.FileSystemObjectをCharsetで_autodetect_allにするには?実際、私のiniファイルは、異なる文字コードでエンコードすることができます。 –
デフォルトでは、 'opentextfile'メソッドはテキストファイルをASCII形式で開きます。 UNICODE形式で開くには、 'Set ofile = ofso.OpenTextFile(strFilePath、1、False、1)'という行を使います。その他のヘルプ:https://msdn.microsoft.com/en-us/library/aa265347(v=vs.60).aspx – Gurman
残念ながら、iniファイルがUTF-16LEとしてエンコードされていると、あなたのメソッド –