Search Field For Specific Text & Select Characters After Said Text Follow

0
Avatar
Legacy Poster

Hi all

 

I'm a noob to BarTender VB Scripting and would like to know how I would go about setting up a script to search through the text of a field and select a specific combination of letters plus "x" nubmer of characters after that text.

 

eg. of text I might encounter:   'The samples were placed in container DNA 128:E08 for processing post assay setup...'  What I'm interested in pulling from this text is purely "DNA 128:E08".

 

I initially created an action under "Search and Replace: " in the Transforms field of the Text Properties using Search and Delete Everything Before (search criteria is "DNA") which gives me what I want but also all text following the 11 characters I desire.  I then tried combining a limitation of 11 characters under "Number of Characters:" transformation but that still recognized the data being deleted by the Search and Delete Everything Before criteria, thus nothing appears in my field.

 

I figured I could use VB Script to find text DNA and then only take the number of characters post that search criteria I desire, but I'm having a little trouble understanding the syntax.

 

Any help would be greatly appreciated.

 

Kind regards,

 

Nickolas.

 

 

3 comments

0
Avatar
Legacy Poster
Comment actions Permalink

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
Comment actions Permalink

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
Comment actions Permalink

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.

 

 

Please sign in to leave a comment.