RegEx Error

Hi everybody,

i try to create a Dateinput-Validation. With RegExRX from @Kem Tekinay i created the Searchstring. Everything is working fine within his program.
I took a Textfield and wrote the following Code to the LostFocus-Method:

[code] Dim input As String = Me.Text.Trim

If input <> “” Then
Dim rx As New RegEx

rx.SearchPattern = "^(\\d+)?\\s*" +_ // Day / Month
"(?:(?:\\/|\\.|\\-\\s*)?(\\w+)?(?:\\/|\\.|\\-\\s*)?)\\s*" +_ // Month
"(\\d+)?" +_ // Year
"(?:\\s*\\/\\s*(\\d{1,2}))?" +_ // 2. Year
"(?:\\s*(B\\.C\\.|BC))?$" // before Christ

Dim rxOptions As RegExOptions = rx.Options

rxOptions.LineEndType = 4
rxOptions.MatchEmpty = True

Dim match As RegExMatch = rx.Search( input )

If match <> Nil Then
  Dim day, month, year, year2, bc As String
  
  day = match.SubExpressionString(1)
  month = match.SubExpressionString(2)
  year = match.SubExpressionString(3)
  year2 = match.SubExpressionString(4)
  bc = match.SubExpressionString(5)
  
  MsgBox day + " " + month + " " + year + " " + year2 + " " + bc
End If

End If
[/code]
…But i will get an Exception out of Bounds, but within @Kem Tekinay Program it’s working fine. Wheres the error?
[h]Sample-Dates[/h]

  • 2000
  • 1/2000 or 2/2000
  • 1.1.2000 or 1/1/2000
  • 1 January 2000 etc.
  • 2000 B.C.

Thank you all :wink:

Well, take the first sample date: 2000. No day, no month, etc. You have to use match.SubExpressionCount to know how many SubExpressionString are returned.

But why, Kem’s program can also show zero-matches :frowning:

RegExRX shows the same thing when you try to match against “2000”. There is only one subgroup so trying to access SubExpressionString( 2 ) in that case would lead to the error you’ve noted.

[quote]Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
Jamie Zawinski[/quote]

[quote=207573:@Eli Ott]Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
Jamie Zawinski[/quote]

LOL!! :stuck_out_tongue:

[quote=207573:@Eli Ott][/quote]
Could be, but not every Xojo-User is a professional. Please, don’t make me feel stupid!

I don’t think this quote was meant towards amateur and semi-professionals, it was more towards the craziness of regular expressions per se.

Martin, that’s an old quote and is applicable to everyone. Even me sometimes. :slight_smile:

@Martin Trippensee Sorry if my message sounds bad to you. It wasn’t my intention! Just that I never heard of that quote and found it really funny. Kem is Xojo Regex Ninja Master and found it funny.

The next time I give an XDC presentation, I’m going to list my official title as “RegEx Ninja Master”.

You could shorten Kem Regex to Kem Rex and sign like a real king would: Kem R. Much better than Kem T.

I’ll give your suggestion all the consideration it merits…

[quote=207618:@Kem Tekinay]The next time I give an XDC presentation, I’m going to list my official title as “RegEx Ninja Master”.
[/quote]

My marketing background … :stuck_out_tongue:

T stands for Tsar (which is actually higher than King) :wink:

Ninja is way far better than Tsar or T. How you dare!? :slight_smile:

With TRex you have older roots.