2016-01-11 18 views
-1

我正在學習正則表達式和vbscript,以便通過每月添加用戶輸入的文本將文本附加到新行上的.c文件。我接收一個正則表達式語法錯誤上行:68字符:1下面的VBScript,以及需要幫助分揀出來:正則表達式語法錯誤代碼:800A1399

path = "<C:\Users\Parth\Desktop\C06S3000.C>" 
Set re = New RegExp 

inputstr1 = InputBox("enter short name for recovery month, example: dec2015") 
re.Pattern = "^([a-zA-Z]{3,5}\d{4})$" 
re.IgnoreCase = True 
Do While re.Test(inputstr1) <> True 
    inputstr1 = InputBox("incorrect entry; enter short name for recovery month, example: dec2015") 
Loop 

inputstr2 = InputBox("enter full name for recovery month, example: december_2015") 
re.Pattern = "^([a-zA-Z_]{3,10}\d{4})$" 
re.IgnoreCase = True 
Do While re.Test(inputstr2) <> True 
    inputstr2 = InputBox("incorrect entry; enter full name for recovery month, example: december_2015") 
Loop 

inputstr3 = InputBox("enter names of all affected groups for recovery month separated by commas and single space, example: a1, a2, a3,...") 
re.Pattern = "^(([a-zA-Z_]{1,2}\d{1,2}[,]\s)*)$" 
re.IgnoreCase = True 
Do While re.Test(inputstr3) <> True 
    inputstr3 = InputBox("incorrect entry, enter names of all affected groups for recovery month separated by commas and space, example: a1, a2, a3,... ") 
Loop 
grpString1 = split(inputstr3, ",") 

inputstr4 = InputBox("enter loss percentage for each group separated by commas and a space; example: 0.00283148378304, 0.39331380134641, ...") 
re.Pattern = "^((\d\.\d{14}[,]\s)*)$" 
Do While re.Test(inputstr4) <> True 
    inputstr4 = InputBox("incorrect entry, enter loss percentage for each group separated by commas and a space; example: 0.00283148378304, 0.39331380134641, ... ") 
Loop 
grpString2 = split(inputstr4, ",") 

If UBound(grpString1) = UBound(grpString2) Then 
Else 
    'use more efficient code by implementing class variables with a function, for example, in future versions' 
    'source: https://bytes.com/topic/asp-classic/answers/125942-vbscript-function-returning-multiple-values' 

    response.write("affected group count does not match loss percentage count; reenter these values to match count") 
    inputstr3 = InputBox("enter names of all affected groups for recovery month separated by commas and single space, example: a1, a2, a3,...") 
    re.Pattern = "^(([a-zA-Z_]{1,2}\d{1,2}[,]\s)*)$" 
    re.IgnoreCase = True 
    Do While re.Test(inputstr3) <> True 
    inputstr3 = InputBox("incorrect entry, enter names of all affected groups for recovery month separated by commas and space, example: a1, a2, a3,... ") 
    Loop 
    grpString1 = Split(inputstr3, ",") 

    inputstr4 = InputBox("enter loss percentage for each group separated by commas and a space; example: 0.00283148378304, 0.39331380134641, ...") 
    re.Pattern = "^((\d\.\d{14}[,]\s)*)$" 
    Do While re.Test(inputstr4) <> True 
    inputstr4 = InputBox("incorrect entry, enter loss percentage for each group separated by commas and a space; example: 0.00283148378304, 0.39331380134641, ... ") 
    Loop 
    grpString2 = Split(inputstr4, ",") 
End If 

Set objfso = CreateObject("Scripting.FileSystemObject") 
If objfso.FileExists(path) Then 
    Set objFile = objFSO.OpenTextFile(path).ReadAll 
End If 

'source: http://www.tutorialspoint.com/vbscript/vbscript_split_function.htm' 
ublptm = UBound(grpString1) 
For i=0 To ublptm 'where lptm = loss_pct_avg_monthyear[group] = percent;' 
    lptmStr = lptmstr + "loss_pct_through_[" & grpString1(i) & "] = " & grpString2(i) & ";" & vbCrLf 
Next 

re.Pattern = "(?<=loss_pct_through_([a-zA-Z]{3,5}\d{4})\[([a-zA-Z_]{1,2}\d{1,2})\]\s=\s\d\.\d{14}[;]\n)\n(?=\}\n)" 

'source: http://stackoverflow.com/questions/13588622/using-vbscript-to-find-and-replace-all-multi-line-text-between-curly-braces-of-a' 

objFile = re.Replace(objFile, vbCrLf & lptmstr & vbCrLf) 

其中線68是:

objFile = re.Replace(objFile, vbCrLf & lptmstr & vbCrLf) 

的正則表達式模式應該尋求.C代碼和上一個新行追加用戶輸入以換行符開始如下:

原始文件:

loss_pct_through_nov2015[a4] = 0.13155605112872; 
loss_pct_through_nov2015[a5] = 0.23415898757080; 

loss_pct_through_dec2015[a2] = 0.00283148378304; 
loss_pct_through_dec2015[a3] = 0.39331380134641; 
loss_pct_through_dec2015[a4] = 0.56333929692615; 
loss_pct_through_dec2015[a5] = 0.04051541794440; <-append content from here 
\n 
} 

更新文件:

loss_pct_through_nov2015[a4] = 0.13155605112872; 
loss_pct_through_nov2015[a5] = 0.23415898757080; 

loss_pct_through_dec2015[a2] = 0.00283148378304; 
loss_pct_through_dec2015[a3] = 0.39331380134641; 
loss_pct_through_dec2015[a4] = 0.56333929692615; 
loss_pct_through_dec2015[a5] = 0.04051541794440; 
\n <--new newline character replacing the old one to append content below 
loss_pct_through_jan2016[a2] = 0.04051541794440; 
loss_pct_through_jan2016[a4] = 0.04051541794440; 

} 
+2

你能簡化你的例子嗎? –

+1

與C無關 - 處理文本的語言無關緊要。並提供[mcve]。網站規則要求問題是獨立的。 – Olaf

+0

混亂的道歉。我的意圖是提供詳細信息以解決問題。我將適當地清理未來的帖子。 – sda

回答

1

你有一個positive lookbehind assertion(?<=...))在這個表達式的開頭:

re.Pattern = "(?<=loss_pct_through_([a-zA-Z]{3,5}\d{4})\[([a-zA-Z_]{1,2}\d{1,2})\]\s=\s\d\.\d{14}[;]\n)\n(?=\}\n)" 

VBScript的正則表達式引擎不支持這些。

+0

如果不存在解決方法,哪種語言最適合完成我的任務,因爲我需要使用「積極回顧」來找到正確的模式。另外,感謝您的編輯! – sda

+0

@Parth Lookaround聲明似乎受[.NET正則表達式引擎](https://msdn.microsoft.com/en-us/library/az24scfc.aspx)支持,因此您應該可以在PowerShell中使用它們。 –