(我需要更改数百行。数据就像这样
activity_id equation 5225518 D0312_ABC * (S3765+S3790+S3762+S3763+S3770+S3764+S4480) * (1-(S2820+S0560))*(1-S1965)*(1-C0151) 7306234 D0300_BCD * C0502 * (1-(S0191))*(1-S1965) 8293425 D0798_CDE * P0692 * (1-(S0191+S2820+S0560+S0290+S0960))*(1-S1965) 9119429 D0793_DEF * S2605 * (1-S0290)*(1-S1965)
我需要将两个“1-something”结合在一起,并且有一个模式。
什么时候有(1-(Sxxx+Sxxxx))*(1-(Sxxx+Sxxxx))
,
将它们组合成一个(1-(Sxxx+Sxxxx +Sxxx+Sxxxx))
所以我只需要用Sxxxx改变一切,你可以忽略Cxxxx,Dxxxx,Pxxxx,......
所以我需要将其更改为:
activity_id equation 5225518 D0312_ABC * (S3765+S3790+S3762+S3763+S3770+S3764+S4480) * (1-(S2820+S0560+S1965))*(1-C0151) 7306234 D0300_BCD * C0502 * (1-(S0191+S1965)) 8293425 D0798_CDE * P0692 * (1-(S0191+S2820+S0560+S0290+S0960+S1965)) 9119429 D0793_DEF * S2605 * (1-(S0290+S1965))
答案 0 :(得分:2)
以下的正则表达当然可以并且可以说应该得到改进。
Option Explicit
Private m_Rex As RegExp
Private Const SEARCH_PATTERN As String = "\(1-\(?((S\d{4}\+?)+)\)?\)\*\(1-\(?((S\d{4}\+?)+)\)?\)"
' $0 $2
Private Const REPLACE_PATTERN As String = "(1-($1+$3))"
Public Function Simplify(ByVal AVeryParticularFormula As String) As String
If m_Rex Is Nothing Then
Set m_Rex = New RegExp
m_Rex.Global = False
m_Rex.MultiLine = False
m_Rex.IgnoreCase = False
m_Rex.Pattern = SEARCH_PATTERN
End If
Do
Simplify = m_Rex.Replace(AVeryParticularFormula, REPLACE_PATTERN)
If Simplify = AVeryParticularFormula Then Exit Do
AVeryParticularFormula = Simplify
Loop
End Function
? Simplify("D0312_ABC * (S3765+S3790+S3762+S3763+S3770+S3764+S4480) * (1-(S2820+S0560))*(1-S1965)*(1-C0151)")
D0312_ABC * (S3765+S3790+S3762+S3763+S3770+S3764+S4480) * (1-(S2820+S0560+S1965))*(1-C0151)
? Simplify("D0300_BCD * C0502 * (1-(S0191))*(1-S1965)")
D0300_BCD * C0502 * (1-(S0191+S1965))
? Simplify("D0798_CDE * P0692 * (1-(S0191+S2820+S0560+S0290+S0960))*(1-S1965)")
D0798_CDE * P0692 * (1-(S0191+S2820+S0560+S0290+S0960+S1965))
? Simplify("D0793_DEF * S2605 * (1-S0290)*(1-S1965)")
D0793_DEF * S2605 * (1-(S0290+S1965))