locked
VB Script Help !! RRS feed

  • Question

  • I am using a piece of software that extracts all text from a document. I am running the following vb script to find a reference number beginning with 05 or 06 then it gives me the numbers that follow eg 0567665 will give me 67665. The problem I have is I need it to give me the whole number including the 05 or 06 .

    thanks for your help !!

    the outputted text before scanning is in ocr_zone01 in our software , this can be ignored.

    ocrText = Metadata.Values("OCR_ZONE01")
    'Variables contain the regular expressions that will be use to look for the required information, will need to be modified when more combinations may occur
    Dim Target, Target2
    Target = ".*(05|06)([A-z]*[0-9]+)"
    Target2 = ""
    Dim arrLines
    arrLines = Split(ocrText, "\r\n")
    call Metadata.SetValues("MY_TEXT", "## NOT FOUND ##")
    'call Metadata.SetValues("MY_SUPPLIER", "## NOT FOUND ##")
    Dim matchedValue
    'Then you can iterate it like this
    For Each strline in arrLines
     match = GetFirstMatch(target, strline)
     If match <> "" Then
      call Metadata.SetValues("MY_TEXT", match)
     End If
     'match = GetFirstMatch(target2, strline)
     'If match <> "" Then
     ' call Metadata.SetValues("MY_SUPPLIER", match)
     'End If
    Next

    ' Get the first objRE  submatch from the string
    ' Returns empty string if not found, otherwise returns the matched string
    Function GetFirstMatch(PatternToMatch, StringToSearch)
     Dim objRE , CurrentMatch, objMatch
     Set objRE  = New RegExp
     objRE.Pattern = PatternToMatch
     objRE.IgnoreCase = True
     objRE.Global = False
     Set objMatch = objRE.Execute(StringToSearch)
     
     GetFirstMatch = ""
     ' We should get only 1 match since the Global property is FALSE
     If objMatch.Count = 1 Then
      ' Item(0) is the (first and only) matching target parts,
      ' Submatches(1) is the substring between the second set of
      ' parentheses (all indexes are zero based)
      GetFirstMatch = objMatch.Item(0).Submatches(1)
     End If
      
     Set objRE  = Nothing
    End Function
    Tuesday, January 31, 2017 1:57 PM

Answers

  • Hi Ian,

    not much of a VB expert, but this should do the trick:

    ' Old Line
    Target = ".*(05)([A-z]*[0-9]+)"
    
    ' New Line
    Target = ".*(05|06|07)([A-z]*[0-9]+)"

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Ian Wallace71 Tuesday, January 31, 2017 3:44 PM
    Tuesday, January 31, 2017 11:21 AM
  • Well ... it's not a clean solution, but I think this may work:

    ' Old Line
    GetFirstMatch = objMatch.Item(0).Submatches(1)
    
    # New Line
    GetFirstMatch = objMatch.Item(0).Submatches(0) + objMatch.Item(0).Submatches(1)

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Ian Wallace71 Tuesday, January 31, 2017 3:44 PM
    Tuesday, January 31, 2017 2:34 PM

All replies

  • Hi

    I am using a piece of software that extracts all text from a document. I am running the following vb script to find a reference number beginning with 05 then it gives me the numbers that follow eg 0567665. the problem I have is some documents start with 06 or 07.

    question : how do I adapt my vb script which does work for 05 but get it to also find o6 numbers or 07 numbers ?

    thanks for your help !!

    the outputted text before scanning is in ocr_zone01 in our software , this can be ignored.

    ocrText = Metadata.Values("OCR_ZONE01")
    'Variables contain the regular expressions that will be use to look for the required information, will need to be modified when more combinations may occur
    Dim Target, Target2
    Target = ".*(05)([A-z]*[0-9]+)"
    Target2 = ""
    Dim arrLines
    arrLines = Split(ocrText, "\r\n")
    call Metadata.SetValues("MY_TEXT", "## NOT FOUND ##")
    'call Metadata.SetValues("MY_SUPPLIER", "## NOT FOUND ##")
    Dim matchedValue
    'Then you can iterate it like this
    For Each strline in arrLines
     match = GetFirstMatch(target, strline)
     If match <> "" Then
      call Metadata.SetValues("MY_TEXT", match)
     End If
     'match = GetFirstMatch(target2, strline)
     'If match <> "" Then
     ' call Metadata.SetValues("MY_SUPPLIER", match)
     'End If
    Next

    ' Get the first objRE  submatch from the string
    ' Returns empty string if not found, otherwise returns the matched string
    Function GetFirstMatch(PatternToMatch, StringToSearch)
     Dim objRE , CurrentMatch, objMatch
     Set objRE  = New RegExp
     objRE.Pattern = PatternToMatch
     objRE.IgnoreCase = True
     objRE.Global = False
     Set objMatch = objRE.Execute(StringToSearch)
     
     GetFirstMatch = ""
     ' We should get only 1 match since the Global property is FALSE
     If objMatch.Count = 1 Then
      ' Item(0) is the (first and only) matching target parts,
      ' Submatches(1) is the substring between the second set of
      ' parentheses (all indexes are zero based)
      GetFirstMatch = objMatch.Item(0).Submatches(1)
     End If
      
     Set objRE  = Nothing
    End Function

    • Merged by Bill_Stewart Tuesday, January 31, 2017 2:11 PM Duplicate
    Tuesday, January 31, 2017 11:10 AM
  • Hi Ian,

    not much of a VB expert, but this should do the trick:

    ' Old Line
    Target = ".*(05)([A-z]*[0-9]+)"
    
    ' New Line
    Target = ".*(05|06|07)([A-z]*[0-9]+)"

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Ian Wallace71 Tuesday, January 31, 2017 3:44 PM
    Tuesday, January 31, 2017 11:21 AM
  • Great, that seems to work.

    This script now gives me the numbers found that come after 05,06 which works a treat.

    is it now possible to edit the script to output the whole number including the 05 rather than just the numbers after it ?

    eg 05876789 instead of the current script giving me 876789

    thanks for your help.

    Tuesday, January 31, 2017 1:04 PM
  • Well ... it's not a clean solution, but I think this may work:

    ' Old Line
    GetFirstMatch = objMatch.Item(0).Submatches(1)
    
    # New Line
    GetFirstMatch = objMatch.Item(0).Submatches(0) + objMatch.Item(0).Submatches(1)

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Ian Wallace71 Tuesday, January 31, 2017 3:44 PM
    Tuesday, January 31, 2017 2:34 PM
  • Fantastic, it worked. Thanks for all your help Fred !!
    Tuesday, January 31, 2017 3:45 PM