Skip to main content

Search

Search

Search Field For Specific Text & Select Characters After Said Text

Comments

3 comments

  • Avatar
    Legacy Poster

    Hello,

     

    I would suggest looking into InString in combination with the Mid function.

    Say for instance I want to know what the position is of the first occerence of "DNA" in the string "I eat DNA for breakfast".

    My code would look like:

    Dim startPosition
    Dim myString
    Dim secondString
    
    myString = "I eat DNA for breakfast."
    
    'Check if the search string is found
    If Not InStr(myString, "DNA") = 0 Then
        'If found then add 3 to the start position
        startPositionNumber = InStr(myString, "DNA") + 3
        'return everything after the start position
        secondString = Mid(myString, startPositionNumber)
        MsgBox(secondString)
    End If
    

     

    Hope this helps.

    0
  • Avatar
    Ian Cummings
    Moderator

    Without going into the specifics, you could also make use of the Regular Expression object of VB script.  Here is an example for validating a UK postal code.

     

    UK Postcode validation:
     
    For this I created a new named data source, not contained in any label object, in order to make use of the OnPostPrompt event for the postcode validation.  I created a function called RegExpTest() which creates the regular expression object to execute against an industry standard UK postcode regular expression pattern.  I then call the function on the OnPostPrompt event cancelling the print job if the post code is found to be invalid.
     
    Functions and Subs:
     
    Function RegExpTest(strng)
         
         'Create variables.
         Dim patrn, regEx, Match, Matches   
         
         'Define the regular expression pattern string.
         patrn = "(GIR 0AA)|((([ABCDEFGHIJKLMNOPRSTUWYZ][0-9][0-9]?)|(([ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0-9][0-9]?)|" & _
                        "(([ABCDEFGHIJKLMNOPRSTUWYZ][0-9][ABCDEFGHJKSTUW])|([ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0-9][ABEHMNPRVWXY]))))" & _
                        " [0-9][ABDEFGHJLNPQRSTUWXYZ]{2})"
    
         'Create a regular expression object and define its properties.
         Set regEx = New RegExp
         regEx.Pattern = patrn
         regEx.IgnoreCase = True
         regEx.Global = False
    
         'Execute the search.
         Set Matches = regEx.Execute(strng)
    
         'Handle any invalid matches returned on the search.
         If Matches.Count = 0 Then
              Call Format.CancelPrinting("Postcode: " & strng & " is invalid!","")
         Else
              If Matches.item(0).FirstIndex > 0 Or Matches.item(0).Length > Len(strng) Then
                   Call Format.CancelPrinting("Postcode: " & strng & " is invalid!","")
              End if
         End If
    
    End Function
    
    

     

    OnPostPrompt:
     
    Call RegExpTest (Format.NamedSubStrings("btPostcode").Value)
    

     

    0
  • Avatar
    Legacy Poster

    Thank you @WvDriel & @Ian C - Technical Ninja at Seagull for you quick response.  Tried both your solutions and they were able to obtain the information I wanted but I'd found an "easier" path (by easier I mean for someone with not much scripting experince).

     

    Because I'd already "Transformed" the data in the Text Properties box using a "Search & Replace:" action by deleting everything before the text "DNA", I only had to concentrate on removing all data after the characters I desired.  I wrote the simple script value = left(value,11) and this seemd to do the job. 

     

    For the following text in the variable field :  "The_Hoff_is_always_right_about_DNA_123:A01_being_where_it_should_be...." I was able to delete "The_Hoff_is_always_right_about_" and then pick only the next 11 characters to display i.e. DNA_123:A01.

     

    I haven't tested it to see what happens if the text block contains 2 separate versions of the search criteria "DNA" though.  I assume it picks the 1st one in the line and runs with that, in which case I may need to implement your more advance scripts and include the pattern condition detailed in Ian C - Technical Ninja at Seagull response.

     

    Either way, thanks for both for your responses.  They are very much appreciated.

     

    Regards,

     

    Nickolas.

     

     

    0

Please sign in to leave a comment.