Regular Expressions Help

G

Guest 20221009

Guest
Hi all,

Need help with Regex in C#, this a bit foreign to me. Need to retrieve First names from a table row with lost of junk in it and I cant seem to get my regex to match.

First Name : rba
c : sd
Company Name : asdfasdf
Email : h@a.com
Cellphone : 2323
Work phone : 2323

Using Rad Regex Designer, I came up with "First Name : (.*)\r\s". It validates, but when I use it in a console app nothing happens..

Please help :confused::confused:
 

dequadin

Expert Member
Joined
May 9, 2008
Messages
1,434
Whenever I hear regular expressions it makes me think of this :D

Sorry that's all I have to offer...
 
G

Guest 20221009

Guest
lol :D ...its hopeless. I need to get this done before 13:00 and our main dev is on leave today..ARRRG!
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
Hi all,

Need help with Regex in C#, this a bit foreign to me. Need to retrieve First names from a table row with lost of junk in it and I cant seem to get my regex to match.



Using Rad Regex Designer, I came up with "First Name : (.*)\r\s". It validates, but when I use it in a console app nothing happens..

Please help :confused::confused:

What's the code you use in the console app to match the expression?
 
G

Guest 20221009

Guest
What's the code you use in the console app to match the expression?


Regex r;
Match m;

string st = "First Name : reba" ;

r = new Regex(@"First Name : (.*)\r\s",
RegexOptions.IgnoreCase);




for (m = r.Match(st); m.Success; m = m.NextMatch())
{
Console.WriteLine("Found href " + m.Groups[0] + " at "
+ m.Groups[0].Index);
}

I just dont get it.. all the examples I have seen dont make any sense.
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
I just dont get it.. all the examples I have seen dont make any sense.

The problem is that you test string does not contain any whitespace or carriage returns after the name string. Your regular expression expects a string in the following format:

First[SPACE]Name[SPACE]:[SPACE][ANY STRING VALUE][CARRIAGE RETURN][WHITE SPACE]

Try modifying it to:

First Name : (.*)\r*\s*

That will allow it to match the first name with or without a carriage return and whitespace after it... ;)
 
G

Guest 20221009

Guest
The problem is that you test string does not contain any whitespace or carriage returns after the name string. Your regular expression expects a string in the following format:

First[SPACE]Name[SPACE]:[SPACE][ANY STRING VALUE][CARRIAGE RETURN][WHITE SPACE]

Try modifying it to:

First Name : (.*)\r*\s*

That will allow it to match the first name with or without a carriage return and whitespace after it... ;)


Many thanks..it worked. Phew! :D
 
Top