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]
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.
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=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]
@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.