Jump to content

Is this proper regular expression behaviour?


therks
 Share

Recommended Posts

So I have this pattern: 

^(?:(\d+)|(\d+):(\d+)|(\d+):(\d+):(\d+))$

And I'm expecting (depending on input) to get a 1, 2 or 3 index array (or @error for invalid input).

But instead I get this:

#include <Debug.au3>

Func Test($String)
    _DebugArrayDisplay(StringRegExp($String, '^(?:(\d+)|(\d+):(\d+)|(\d+):(\d+):(\d+))$', 1))
EndFunc

Test('10')
; Results (normal, expected):
; Row 0|10

Test('10:20')
; Results (extra blank index):
; Row 0|
; Row 1|10
; Row 2|20

Test('10:20:30')
; Results (three blank indices):
; Row 0|
; Row 1|
; Row 2|
; Row 3|10
; Row 4|20
; Row 5|30

Is this normal? Should I just code around it, or is there a better way to do what I'm looking for?

I also tried reversing my regex, but it was even uglier results:

#include <Debug.au3>

Func Test($String)
    _DebugArrayDisplay(StringRegExp($String, '^(?:(\d+):(\d+):(\d+))|(\d+):(\d+)|(\d+)$', 1))
EndFunc

Test('10')
; Results (yuck):
; Row 0|
; Row 1|
; Row 2|
; Row 3|
; Row 4|
; Row 5|10

Test('10:20')
; Results (slightly better):
; Row 0|
; Row 1|
; Row 2|
; Row 3|10
; Row 4|20

Test('10:20:30')
; Results (nice):
; Row 0|10
; Row 1|20
; Row 2|30

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...