WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Strings

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Regular Expression Search for String Pattern


Question:

I am trying to use StrSubWild to locate a substring. It looks like it doesn't find the wildcarded string I am expecting.

;Test-StrSubWild.wbt
data = "AutoSampler Report   Table Name: STL              Autosampler Type: TYPE TJA Set#  Type      Prepare?  Description           Method    #Pos  Rack#  StartPos1   Normal      No                            P40207A     48     2      1"
pos = StrIndexWild(data, "P?????", 1) 
ret = StrSubWild(data, "P?????", pos)
Message("StrSubWild result", ret)
Exit 
Example --> P?????? would find the first 'P' regardless of case. And it will include spaces. I need to find a string in this format: "P40207A".

Answer:

Sound like you need more sophistacated string searching capabily then the StrSubWild function can offer. You need to use Regular Expression string searching. Here is an example:
#DefineFunction SimpleRegEx( myString, myExpression, ignoreCase )
   regex = ObjectCreate("VBScript.RegExp") ; Creates a regular expression object for use by WinBatch.
   regex.Pattern = myExpression  ; Required to establish a regular expression pattern to use in searching.
   regex.IgnoreCase = ignoreCase ; Optional, but required by this user defined function.
   If regex.Test(myString) == 0 ; Set default return values if search string was not found.
		index = -1
		position = 0
		value = ""
		length = -1 
   Else 
		objCollection = regex.Execute(myString) ; This initiates the object collection of the regular expression object. (!)
		henum = ObjectCollectionOpen(objCollection) ; OK. Start 
		match = ObjectCollectionNext(henum)
		index = match.FirstIndex  ; 0 based index. Note that WIL variables are indexed at 1. Arrays are indexed at 0.
		position = index + 1
		value = match.Value
		length = match.Length
		ObjectCollectionClose(henum)
   Endif
   list = StrCat(value,@TAB,position,@TAB,length) ; Position is the normal WIL use based on 1 as the first character.
   ObjectClose(regex)
   Return list
#EndFunction

data = "AutoSampler Report   Table Name: STL              Autosampler Type: TYPE TJA Set#  Type      Prepare?  Description           Method    #Pos  Rack#  StartPos1   Normal      No                            P40207A     48     2      1"
regstr =  "P\d{5}[A-Z]"
ret = SimpleRegEx(data,regstr,@false)
value = ItemExtract(1, ret, @tab)
Message("Search result", value)
Exit 


Reference:
Regular Expression Object Properties and Methods (VBScript)
http://msdn.microsoft.com/en-us/library/kxt24tyh(v=vs.84).aspx


Article ID:   W17267
File Created: 2012:10:26:08:26:56
Last Updated: 2012:10:26:08:26:56