WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: spl on April 11, 2025, 07:09:46 AM

Title: OT: Would Regex in WB switch make any sense
Post by: spl on April 11, 2025, 07:09:46 AM
Curious... assume iterating lines from a web response where users asked to indicate 5 cities from best to worst but you were interested in seeing if San Fran came on top and Miami worst, but if New York and LosAngeles were included... your get

"San Francisco a great city, so is new york, not so much LOS ANGELES, Dallas is ok, but worst is miami"
"Detroit a great city, so is new york, not so much Cleveland, Dallas is ok, but worst is Hoboken"

Could probably we figured out through a series of if statememts, but would WB parse each line as a switch using regex. [I am looking at WB switch has to return a number, so regex should either return 0 or 1]

something like [pseudo code]
foreach line in lines
Switch line
   Case regex("^San Francisco")
      Message("Switch", "San Francisco the best") ;
   Case regex("new york")
      Message("Switch", "New York")
   Case regex("Los Angeles")
      Message("Switch", "Los Angeles")
  Case regex("Miami")
      Message("Switch", "Miami the worst")
   Case line ; default case
      Message("Switch", "Not found")
      Break
EndSwitch
Next

which would involve setting up a regex() udf to return 1 or 0. I know this might look stupid but switch might be  faster than any alternative.



Title: Re: OT: Would Regex in WB switch make any sense
Post by: td on April 11, 2025, 09:00:47 AM
Like case statements, the switch statement can only accept expressions that evaluate to an integer. You would need to do something like

switch 1
case foobar
case foobar
case foobar
case 1
endswitch
Title: Re: OT: Would Regex in WB switch make any sense
Post by: spl on April 11, 2025, 09:09:37 AM
Asked and answered.
Title: Re: OT: Would Regex in WB switch make any sense
Post by: spl on April 12, 2025, 11:22:38 AM
The goal is more sophisticated than the example below. I mistakenly forgot the switch expression needed to be an integer rather than case statements. The multiple if's do work and the regex is set to be case insensitive. Looking for more complex test of a long string or file, i.e. does it contain

with more than one regex possible. Probably switch [no pun intended] to a map, array, or db to push the regex into a function along with the text to parse. ITMT: some reading this might might get an idea from the code.
IntControl(73,1,0,0,0)
gosub udfs
ObjectClrOption( 'useany', 'System')
line = "San Francisco a great city, so is new york, not so much LOS ANGELES, Dallas is ok, but worst is miami"

results = ""
if regex(line,"^San Francisco")
   results := "San Francisco ranked best":@CRLF
elseif regex(line,"San Francisco")
   results := "San Francisco is in the top 5":@CRLF
endif
if regex(line,"New York") Then results := "New York is in the top 5":@CRLF
if regex(line,"los angeles") Then results := "Los Angeles is in the top 5":@CRLF
if regex(line,"Miami$")
   results := "Miami ranked worst":@CRLF
elseif regex(line,"Miami")
   results := "Miami is in the top 5":@CRLF
endif

Message("results",results)
Exit

:WBERRORHANDLER
geterror()
Terminate(@TRUE,"Error Encountered",errmsg)

;=====================================================
:udfs
#DefineSubRoutine geterror()
   wberroradditionalinfo = wberrorarray[6]
   lasterr = wberrorarray[0]
   handlerline = wberrorarray[1]
   textstring = wberrorarray[5]
   linenumber = wberrorarray[8]
   errmsg = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
   Return(errmsg)
#EndSubRoutine

#DefineSubRoutine regex(line,pattern)
   retval = 0
   opts = ObjectClrType('System.Text.RegularExpressions.RegexOptions',1)
   oReg = ObjectClrNew('System.Text.RegularExpressions.Regex',pattern,opts)
   oReg.CacheSize = ObjectType("ui2",30)
   m = oReg.IsMatch(line)
   if m<>0 Then retval = 1
   oReg=0
   Return(retval)
#EndSubRoutine

Return