از اینجا به بعد برای تشخیص تکراری ها میتونیم 2 راه در پیش بگیریم:
- یکی این که وقت و انرژی بگذاریم و یک پترن دقیق و در برگیرنده همه حالت ها بسازیم (راه سخت و زمانبر)
- با یکی از پترن های پست 70 یا 80 (یا هر پترن دیگه که شرایط سناریو ما رو برآورده میکن)
یک لیست از همه کلمه ها بسازیم و از روی این لیست تکراری ها رو پیدا کنیم (راه ساده و سریع)
در کد نمونه زیر پس از درست کردن یک لیست از همه کلمه ها،
یک دیکشنری میسازیم و در یک لوپ روی لیست،
اگر یک کلمه در دیکشنری نبود اون رو اضافه میکنیم (بعنوان کلید) و تعداش رو 1 میگذاریم (بعنوان item)،
و اگر از قبل وجود داشت مقدار item رو یکی اضافه میکنیم.
Dim Parts()
Parts = Array( _
"USE IRAN;", _
"WITH ALLCITIES AS (", _
"SELECT City , CityID, DistrictID FROM Cities WHERE CITY IS NOT NULL", _
"UNION", _
"SELECT RuralDistrict AS City, RuralDistrictID AS CityID, DistrictID FROM RuralDistricts WHERE RuralDistrict IS NOT NULL", _
")", _
"SELECT", _
" states.State , states.StateID,", _
" Counties.County , Counties.CountyID,", _
" Districts.District , Districts.DistrictID,", _
" ALLCITIES.City , ALLCITIES.CityID", _
"FROM states", _
" LEFT JOIN Counties ON states.StateID=Counties.StateID", _
" LEFT JOIN Districts ON Counties.CountyID=Districts.CountyID", _
" LEFT JOIN ALLCITIES ON Districts.DistrictID=ALLCITIES.DistrictID", _
"ORDER BY states.State, Counties.County, Districts.District, ALLCITIES.City", _
"COLLATE Persian_100_CI_AI", _
"FOR JSON AUTO ,WITHOUT_ARRAY_WRAPPER;")
Const pattern = "(\S+)"
Dim mc As MatchCollection
Dim m As match
With New RegExp
.Global = True
.IgnoreCase = True
.Multiline = True
.pattern = pattern
Set mc = .Execute(Join(Parts, vbCrLf))
End With
Dim d As New Dictionary
For Each m In mc
If d.Exists(m.Value) Then
d(m.Value) = d(m.Value) + 1
Else
d.Add key:=m.Value, item:=1
End If
Next
Dim item
Dim i As Integer
For i = 0 To d.Count - 1
If d.Items(i) > 1 Then
Debug.Print d.Keys(i), d.Items(i)
End If
Next
Set d = Nothing
ALLCITIES 2
AS 3
SELECT 3
, 5
CityID, 2
DistrictID 2
FROM 3
WHERE 2
IS 2
NOT 2
NULL 2
RuralDistrict 2
ALLCITIES.City 2
LEFT 3
JOIN 3
ON 3