Extract value from Zone OCR using RegEx and script

  • 467 Views
  • Last Post 05 May 2021
  • Topic Is Solved
Anders Hantveit posted this 04 May 2021

Hello, hope one of you can see what I'm struggling with here.....;

I'm trying to extract value from a Zone OCR, using script sample from this link:

https://forum.scanshare.com/thread/script-extracts-a-part-of-a-string-that-corresponds-to-a-regular-expression/

The problem I have is first of all, I'm not experienced in scripting, but normally I understand enough to edit simple examples. This time I was unable to...

The problem I have when using the script is that it will find several matches from my RegEx in the zone, but I should have the value from the first hit. I get the value from the last hit instead, and I don't understand what to change in the script to make it only get the first match.

This is the script (so I have only made some minor changes):

--------------------

'###Variable definition area
'The user need to define the name of the metadata that is used as input sSearch = Metadata.Values("metadata")
 
sSearch = Metadata.Values("OCR_ZONE01")
'The user need to define the regular expression to be used on the metadata sPattern(1) = expression
DIM sPattern(1)
sPattern(1) = "\d\d[\,\.]\d\d[\,\.]\d\d"
FOR I = 1 TO 1
    call Application.Log(20, "Pattern to search: " & sPattern(I))
    call TestRegExp(sPattern(I), sSearch)
NEXT

Sub TestRegExp(sPattern, sSearch)
    ' Do the regular expression match
    Dim oRE, oMatches
    Set oRE = New RegExp
    oRE.Global = True
    oRE.IgnoreCase = True
    oRE.Pattern = sPattern
    Set oMatches = oRE.Execute(sSearch)
    Dim oMatch
    For Each oMatch In oMatches
        call Application.Log(20, "Find part: " & oMatch.Value)
        call Metadata.SetValues("PART_OCR_ZONE01",oMatch.Value)
    Next
End Sub

--------------

Anyone have an idea?

 

Thanks in advance

Best regards, Anders Hantveit

 

 

Senior System Engineer, MCS, PRS and IMS ## Konica Minolta Business Solutions Norway AS

Order By: Standard | Newest | Votes
Anders Hantveit posted this 05 May 2021

Hello, thanks for quick reply!

The "Exit For Next" did the trick !

laughing

Senior System Engineer, MCS, PRS and IMS ## Konica Minolta Business Solutions Norway AS

  • Liked by
  • luca.scarpati
luca.scarpati posted this 04 May 2021

Hi Anders,

 

no problem, you always assume that everything is almost possible in Scanshare laughing.

 

For your script above it depends on what you have in the variable (oMatches) that you loop in the for. From your previous message, I think it is an exit for question so maybe you could solve it with a simple part of code looks like this:

...........

   For Each oMatch In oMatches

        call Application.Log(20, "Find part: " & oMatch.Value)

        call Metadata.SetValues("PART_OCR_ZONE01",oMatch.Value)

        Exit For         

   Next

............

or you can directly access the first element of the array:

............

   Dim oMatch

   oMatch = oMatches(0)

   call Application.Log(20, "Find part: " & oMatch.Value)

   call Metadata.SetValues("PART_OCR_ZONE01",oMatch.Value)

............

 

Best regards,

Luca

luca.scarpati posted this 05 May 2021

Hi Anders,

 

perfect innocent and more than welcome!

 

Best regards,

Luca

Close