RE: Getting only numbers instead of values when using regular expression groups in Visual Basic 2008 express


Ok this will do what you want.  Now remember that visual basic is kind of
stupid in that if you name the region it does not keep it in order.  So they
say if you name the region you need to make sure you either don't care about
the order they are returned in or you always use the named to make sure you
get what you think you will.  Your regex was not getting what you wanted if
you would have looped through your group you would have found that you were
only returning a letter for each of 6 results you were getting.  After I
changed your print to a loop I started tinkering with the regex string and I
now have it returning the first name, last name the start date,  and the end
date with month and the project name which is what I think you wanted.  If
not you will have to tinker a bit more with the regex string I have made.
Here is my code.  

*NOTE*  I notice that good old Microsoft ahs rapped my code so before you
can compile this make sure you put all my regex string on one line.



Imports System.Text.RegularExpressions
Module Module1


    Sub Main()
        Dim RString As String =
"(?<fName>\w+)_(?<lName>\w+)\-(?<StartDay>\w+)to(?<EndDay>\w+)_(?<procname>\
w+).xls"




        Try
            Dim TestRegex As Regex = New Regex(rString,
RegexOptions.IgnoreCase)

            Dim S As String = "pranav_lal-12To23April2008_veta.xls"

            Dim M As Match = TestRegex.Match(S)

            If M.Success Then
                Console.WriteLine("ok")
Console.WriteLine(M.Groups.Count)
            Else
                Console.WriteLine("error")

            End If
            Dim groups As GroupCollection = M.Groups
Dim i As Integer
For i = 1 To groups.Count



            Console.WriteLine(String.Format("Value {0}",
groups.Item(i).Value))
Next
        Catch x As ArgumentException
            Console.WriteLine(x.ToString)
        End Try
        Console.WriteLine("Hit <enter> to exit...")

        Console.ReadLine()


    End Sub

End Module


 

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Pranav Lal
Sent: Friday, May 30, 2008 5:25 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: Getting only numbers instead of values when using regular
expression groups in Visual Basic 2008 express

Hi Ken,
I want to extract different parts of the match. This is why I have made
groups. So, I want to say something like firstName=m.groups("fname").value
and get the first name of the employee. 

Pranav
-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Ken Perry
Sent: Friday, May 30, 2008 9:07 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: Getting only numbers instead of values when using regular
expression groups in Visual Basic 2008 express



Ok ignore that last answer.  I just looked at how the regex stuff works I am
wondering why you did the  Dim group etc.  I just tried your code using  the
following line to print out the match:

Console.WriteLine(String.Format("Value {0}", M.Value))

It writes out what it matched like that so if you tell me what you were
trying to do with the dim as group then I can probably tell you how to do
it.

Ken
 

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Ken Perry
Sent: Friday, May 30, 2008 7:50 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: Getting only numbers instead of values when using regular
expression groups in Visual Basic 2008 express


You need to change your code to use .text rather than .value I think. .

Ken 

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Pranav Lal
Sent: Friday, May 30, 2008 1:58 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: Getting only numbers instead of values when using regular
expression groups in Visual Basic 2008 express

Hi all,

I have a regular expression in visual basic .net 2008. I am using it to
validate the file names of reports I recieve. I want to use groups in
regular expressions to extract information from the file name.
Whenever I access any item of the groups collection though I can only see a
number. I see no names.

My regular expression is as follows.
(?<fName>\w)+_(?<lName>\w)+\-(\w|\D)+to(\w|\d)+_(?<projectName>\w)+\.xls$

my test string looks like
Pranav_Lal-25To30May2008_vita.xls

Any one any ideas? I am posting the full source of the test program below.

Pranav
Imports System.Text.RegularExpressions

Module Module1

    Sub Main()
        Dim rString As String =
"(?<fName>\w)+_(?<lName>\w)+\-(\w|\D)+to(\w|\d)+_(?<projectName>\w)+\.xls$"

        Try
            Dim TestRegex As Regex = New Regex(rString,
RegexOptions.IgnoreCase)

            Dim S As String = "pranav_lal-12To23April2008_veta.xls"

            Dim M As Match = TestRegex.Match(S)
            If M.Success Then
                Console.WriteLine("ok")
            Else
                Console.WriteLine("error")

            End If
            Dim groups As GroupCollection = M.Groups
            Console.WriteLine(groups.Item(1).Value)

        Catch x As ArgumentException
            Console.WriteLine(x.ToString)
        End Try
        Console.WriteLine("Hit <enter> to exit...")

        Console.ReadLine()


    End Sub
End Module
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at 
http://www.freelists.org/list/programmingblind

Other related posts: