Re: Finding duplicates with RegEX



scott schrieb:
For the sample below, I'm trying to construct an expression to capture drive sizes (second word) and the corrisponding serial id (fourth word). The expression I have so far is this : '\w*(\d{2,3}.\dGB).*(\(.*\))'
It only returns part of the drive size (ie: 74.4GB instead of 274.4GB).

That's because you need to denote the literal 'dot' as: \.
instead of an arbitrary character: .

> Can the expression also filter duplicates?

Not directly, but you can handle it yourself, by letting a
function process (sub)matches while calling
RegExp-Replace:


MfG,
Alex



Dim src
Dim dic
Dim r

src = _
"XMTOMC320 274.4GB 512B/sect (A8188BKE)" & vbCrLf _
& "XMTOMC320 136.5GB 512B/sect (A818DNME)" & vbCrLf _
& "XSCHT6146F 68.0GB 520B/sect (3HYX5ERR0000750175VW)" & vbCrLf _
& vbCrLf _
& "XMTOMC320 274.4GB 512B/sect (A8188BKE)" & vbCrLf _
& "XMTOMC320 136.5GB 512B/sect (A818DNME)" & vbCrLf _
& "XSCHT6146F 68.0GB 520B/sect (3HYX5ERR0000750175VW)" & vbCrLf



Set dic = CreateObject("Scripting.Dictionary")
Set r = New RegExp

r.MultiLine = True
r.Global = True

r.Pattern = "^\w+ +(\d{2,3}\.\dGB) +.*\((.+)\)$"

Call r.Replace (src, GetRef("GetDriveSize"))
WSH.Echo Join(dic.Items, vbCrLf)


Sub GetDriveSize (match, s1, s2, pos, src)

Dim key
key = s1 & " " & s2

If Not dic.Exists(key) Then
dic.Add key, "Serial: " & s2 & ", Size: " & s1
End If

End Sub





XMTOMC320 274.4GB 512B/sect (A8188BKE)
XMTOMC320 136.5GB 512B/sect (A818DNME)
XSCHT6146F 68.0GB 520B/sect (3HYX5ERR0000750175VW)
XMTOMC320 488.7GB 512B/sect (A818NGCE)

XMTOMC320 274.4GB 512B/sect (A8188BKE)
XMTOMC320 136.5GB 512B/sect (A818DNME)
XMTOMC320 488.7GB 512B/sect (A818NGCE)
XSCHT6146F 68.0GB 520B/sect (3HYX5ERR0000750175VW)


.



Relevant Pages

  • Re: Looping across two parallel ranges
    ... Dim src as range ... dim tgt as range ... I want to take the text contents of cells in one named range, ...
    (microsoft.public.excel.programming)
  • Re: Looping across two parallel ranges
    ... Dim src as range ... dim tgt as range ... Set src = Range ...
    (microsoft.public.excel.programming)
  • Re: zlib OpennetCF question
    ... Dim source As String = "A quick brown fox jumped over lazy dogs" ... Dim compressedSize As Integer ... Dim ret As ErrorCode = zlibCE.Compress(dst, src, compressedSize) ... Then uncompressedsize should be the length of the original string. ...
    (microsoft.public.pocketpc.developer)
  • Re: extract numbers from text string
    ... Be sure to define Dest and Src appropriately in the VBA Code. ... It looks for sub-sequences which consist of a word that consists only of ... digits, followed by a sequence of words none of which consist of only digits. ... Dim Dest As Range ...
    (microsoft.public.excel.newusers)
  • Re: extract numbers from text string
    ... Be sure to define Dest and Src appropriately in the VBA Code. ... It looks for sub-sequences which consist of a word that consists only of ... digits, followed by a sequence of words none of which consist of only digits. ... Dim Dest As Range ...
    (microsoft.public.excel.newusers)

Loading