2011年4月29日 星期五

[Regex] Regex的迷思,完全比對的作法假設

Regex在比對文件內容時,一般都是將符合規則的過濾出來。
但若想要做到完全比對。則必須用一點小技巧。

把待檢測的字串中有符合的規則的字串取代掉,若還有剩下或多餘的字元,就代表有問題。
此可以避免符合規則下的迷思

因一般來說,我們會用isMatch=true 來取得符合規則的部分
但即使規則正確,但並非完全正確的情形卻不可避免

Public gloWUnitPattern As New Regex("臺灣\d{6}公司")

If DetectExtraInformationForRegex(待測字串, gloWUnitPattern規則) <> 0 Then

End If


'偵測符合Regex格式,但仍有多餘的資訊,需列為必改資料!!
' Public Function DetectExtraInformationForRegex(ByVal s As String, ByVal RegexPattern As String) As Integer
Public Function DetectExtraInformationForRegex(ByVal s As String, ByVal rgx As Regex) As Integer

'Dim rgx As New Regex(RegexPattern)
Dim MatchCount As Integer = 0

For Each match As Match In rgx.Matches(s)
s = s.Replace(match.Value, "")
MatchCount += 1
Next
'若沒有成功 就寫入某字串,讓長度不等於0
If MatchCount = 0 Then
s &= "X"
End If

Return s.Length '回傳值 若不為0 就可能有錯誤!


End Function

[Asp.net ] 使用AccessDataSource控制項,SQL語法的Rnd亂數無法使用

要使用Rnd() 做隨機文章的效果

SqlCmd = " SELECT TOP 7 * FROM [" & SQLTB & "] WHERE VIS = 1 order by Rnd(" & SortCol & ")

使用AccessDataSource控制項,SQL語法的Rnd亂數無法使用
請改用OleDbConnection OleDbCommand等,就可以正常了

2011年4月26日 星期二

追蹤者