Regular Expression Pattern Extractor

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
Hi
Using this REP I wanna extract all emails from a text:
Code:
/[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}/i
And this is a sample text:
Code:
Dave’s email address dave@mysite.com, Mike’s email address mike@example.com,
and Henry’s email address henry@sample.org
It's my first time I am using Regular Expressions!
so I used and customized this code found in MSDN:
Visual Basic:
        Dim text As String = "Dave’s email address [email]dave@mysite.com[/email], Mike’s email address [email]mike@example.com[/email], and Henry’s email address [email]henry@sample.org[/email]"
        Dim pat As String = "/[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}/i"
        ' Compile the regular expression.
        Dim r As Regex = New Regex(pat, RegexOptions.IgnoreCase)
        ' Match the regular expression pattern against a text string.
        Dim m As Match = r.Match(text)
        Dim matchcount As Integer = 0
        While (m.Success)
            matchcount += 1
            MsgBox("Match" & (matchcount))
            Dim i As Integer
            For i = 1 To 2
                Dim g As Group = m.Groups(i)
                MsgBox("Group" & i & "='" & g.ToString() & "'")
                Dim cc As CaptureCollection = g.Captures
                Dim j As Integer
                For j = 0 To cc.Count - 1
                    Dim c As Capture = cc(j)
                    MsgBox("Capture" & j & "='" & c.ToString() _
                       & "', Position=" & c.Index)
                Next j
            Next i
            m = m.NextMatch()
        End While
It won't work!
Am I wrong?
Anyone able to help me with this?:)
 
The problem is that you don't understand regular expressions well enough to realise that the leading / and trailing /i in the pattern you're trying to use are nonsense. I think they're sed syntax but i don't really think it matters particularly, they're clearly not helpful to you.

I stongly suggest you not use regular expression until you have some idea how they work, they're complicated and often subtle and if you rely on them to work without knowing when they're likely to fail you're going to end up missing or corrupting information in the rest of your application sooner or later.
 
Back
Top