viewpoint-particle

Author Topic: Is there a different .NET Regex  (Read 2949 times)

stanl

  • Pundit
  • *****
  • Posts: 777
Is there a different .NET Regex
« on: June 13, 2014, 11:23:11 am »
I tried a WB script to test for an email address with this regex
Code: [Select]
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
Code: [Select]
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

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: Is there a different .NET Regex
« Reply #1 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
;***************************************************************************
;**    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

  • Tech Support
  • *****
  • Posts: 2538
    • WinBatch
Re: Is there a different .NET Regex
« Reply #2 on: June 13, 2014, 12:56:10 pm »
I tried a WB script to test for an email address with this regex
Code: [Select]
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
Code: [Select]
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.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


stanl

  • Pundit
  • *****
  • Posts: 777
Re: Is there a different .NET Regex
« Reply #3 on: June 16, 2014, 06:35:12 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

  • Full Member
  • ***
  • Posts: 184
  • WinBatch rocks, and so does Wilson WindowWare!
    • WizardWrx
Re: Is there a different .NET Regex
« Reply #4 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.
David A. Gray
You are more important than any technology.