Is there a different .NET Regex

Started by stanl, June 13, 2014, 11:23:11 AM

Previous topic - Next topic

stanl

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.


Deana

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.
Deana F.
Technical Support
Wilson WindowWare Inc.

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

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.

DAG_P6

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.
David A. Gray
You are more important than any technology.