WinBatch® Technical Support Forum

Archived Boards => COM Automation and dotNet => Topic started by: stanl on June 13, 2014, 11:23:11 AM

Title: Is there a different .NET Regex
Post by: stanl on June 13, 2014, 11:23:11 AM
I tried a WB script to test for an email address with this regex

oRegex.Pattern = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"


and it works well.  Then I tried this expression (which works in .NET) - note I added the Extra % to avoid a WB substitution error

oRegex.Pattern = "(?i)\b[A-Z0-9._%%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"


but when that pattern applied to the original script I get an OLE Exception. This is more out of curiosity than any issue with WB.

Title: Re: Is there a different .NET Regex
Post by: Deana on June 13, 2014, 11:57:47 AM
I just tested both patterns and it seems to work for me using this code:

Code (winbatch) Select

;***************************************************************************
;**    Email Checker
;**
;** Purpose: Checks that an email address matches a pattern
;** Inputs:
;** Outputs:
;** Reference:
;**       REQUIRES WinBatch 2013A or newer
;**
;** Developer: Deana Falk 2014.06.13
;***************************************************************************
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Load assemblies into the WinBatch process.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ObjectClrOption( 'use', 'System, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089')

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Prompt for input
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;link = ``
;link = `a`         ; NO MATCH
;link = `@.`        ; NO MATCH
;link = `a@`        ; NO MATCH
;link = `a@a`       ; NO MATCH
;link = `a@a.`      ; NO MATCH
;link = `a@a.c`     ; NO MATCH
;link = `1`         ; NO MATCH
;link = `a@a.cc`    ; MATCH
;link = `1@1.com`   ; MATCH
;link = `a@a.com`   ; MATCH
;link = `a.a@a.com` ; MATCH
link = `a_a@a.com`  ; MATCH

pattern = "(?i)\b[A-Z0-9._%%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"
;*OR*
;pattern = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Create a class implemented by a managed assembly.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
oReg = ObjectClrNew('System.Text.RegularExpressions.Regex',pattern)
If oReg.IsMatch( link )
   Message('IsMatch','Match')
Else
   Message('IsMatch','No Match')
EndIf
Exit


Maybe the issue is specific to your code? Post your script and the DebugTrace output, if you need further assistance.
Title: Re: Is there a different .NET Regex
Post by: td on June 13, 2014, 12:56:10 PM
Quote from: stanl on June 13, 2014, 11:23:11 AM
I tried a WB script to test for an email address with this regex

oRegex.Pattern = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"


and it works well.  Then I tried this expression (which works in .NET) - note I added the Extra % to avoid a WB substitution error

oRegex.Pattern = "(?i)\b[A-Z0-9._%%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"


but when that pattern applied to the original script I get an OLE Exception. This is more out of curiosity than any issue with WB.

If by difference you mean a difference between the FCL regex class and the COM Automation regex object then yes, there are is a difference in the supported regex syntax.  The FCL version supports more features including options.
Title: Re: Is there a different .NET Regex
Post by: stanl on June 16, 2014, 06:35:12 AM
Quote from: Deana on June 13, 2014, 11:57:47 AM
I just tested both patterns and it seems to work for me using this code:

I think Tony answered the question, and I probably should have written that the original script was written for COM. Since .NET appears to handle a broader syntax, better to upgrade the older COM script rather than debug regex.
Title: Re: Is there a different .NET Regex
Post by: DAG_P6 on June 20, 2014, 11:19:58 PM
I've never had good experiences with the regular expression engine in the Scripting Runtime library (the COM engine to which you refer). Since I learned regular expressions on the Perl regular expression engine, you could say that I was spoiled. I was thrilled to discover that the .NET engine seems to mirror the Perl engine, and I have yet to throw it an expression that I pulled from a Perl script that didn't behave identically in .NET.

Lots of things in the COM implementation are definitely broken, including newline handling.