2012-02-17 22 views
0

これを理解しようとしていますが、機能しなくなってしまいました。文字列/ txtファイル内の文字列を置換する文字と数値に値を設定する

私はこの

例のようになります.txtファイルがあります:

GA117.50.0117.50.0117.50.0IL16.08.08.00.016.00.0IN284.09.4274.60.0284.00.0KY137を.60.0137.60.0137.60.0TN170.30.0170.30.0170.30.0US725.417.4708.00.0725.40.0TOTAL725.417.4708.00.0725.40.0私はクラシックASPで何をしようとしている

は、文字を取得することです/単語をdimで(そして文字/単語の前にstrを追加する)、数値をthの値として追加する暗いところでは最初の期間にのみ、もう1つは右に数えてから、次の文字/単語に進みます。

ので、最終的な結果は次のようになります。

dim strGA 
dim strIL 
dim strIN 
dim strKY 
dim strTN 
dim strUS 
dim strTOTAL 

strGA=117.5 
strIL=16.0 
strIN=284.0 
strKY=137.6 
strTN=170.3 
strUS=725.4 
strTOTAL=725.4 

をこの問題/疑問を持つ任意の助けをありがとうございました。

+2

これは正規表現には適しています。 –

答えて

3

次の例を考えてみましょう。
変数名に応じてパターンを変更する必要があるかもしれません。

Dim strTest 
    strTest = "GA117.50.0117.50.0117.50.0IL16.08.08.00.016.00.0IN284.09.4274.60.0284.00.0KY137.60.0137.60.0137.60.0TN170.30.0170.30.0170.30.0US725.417.4708.00.0725.40.0TOTAL725.417.4708.00.0725.40.0" 
Dim re 
Set re = New RegExp 
    re.IgnoreCase = True 
    re.Global = True 
    re.Pattern = "([a-z]+)(\d+\.\d)" 

Dim collMatches 
Set collMatches = re.Execute(strTest) 
Dim iMatch, strDims, strAssocs 
For Each iMatch In collMatches 
    strDims = strDims & "Dim str" & iMatch.SubMatches(0) & vbCrLf 
    strAssocs = strAssocs & "str" & iMatch.SubMatches(0) & " = " & iMatch.SubMatches(1) & vbCrLf 
Next 

Dim strExec 
strExec = strDims & vbCrLf & strAssocs 

'Dump 
Response.Write "Dump:<hr /><pre>" & strExec & "<pre>" 

ExecuteGlobal strExec 'Execute the code 

'Test 
With Response 
    .Write "Executed:<hr />" 
    .Write "strGA: " & strGA & "<br />" 
    .Write "strIL: " & strIL & "<br />" 
    .Write "strIN: " & strIN & "<br />" 
    .Write "strKY: " & strKY & "<br />" 
    .Write "strTN: " & strTN & "<br />" 
    .Write "strUS: " & strUS & "<br />" 
    .Write "strTOTAL:" & strTOTAL & "<br />" 
End With 
+0

あなたは大歓迎です。 –

+0

これはどのように実行するのですか?私はstrExec.executeを試しましたが、Object required: 'Dim strGA Dim strIL'というエラーが出ます。再度、感謝します。 – compcobalt

+1

関数/サブルーチンから実行するが、変数はグローバルでなければならない場合は、 'Execute strExec'または' ExecuteGlobal strExec'を実行してください。 –

関連する問題