Regex question

T

tshad

I have a text string that has a date in it and I am trying to pull out the
date. In my earlier posts I got a couple of patterns that seem correct, but
I can't seem to get them to work. I want to account for 1 or 2 digits in
the month and day fields and 2 or 4 in the year field.

After not getting the correct result I put it down to the simplest pattern
which would match exactly the number of digits and I get nothing.

For example, if I have the following string:

valueIn = "At 02/23/2007 DOM";

I want to end up with 02/23/2007.

If I use:

Match oMatch;
oMatch = Regex.Match(valueIn, @"\d{2}/\d{2}/\d{4}");

I get nothing from my oMatch.Groups[1].Value.

If I have:

oMatch = Regex.Match(valueIn, @"\d{1,2}/\d{1,2}/\d{2,4}");
strValue = oMatch.Groups[1].Value;

I get nothing from my oMatch.Groups[1].Value;

If I have:

oMatch = Regex.Match(valueIn,
@"\d{1,2}(-|/)\d{1,2}(-|/)(\d{2}|\d{4})");
strValue = oMatch.Groups[1].Value;

oMatch.Groups[1].Value = "/"
oMatch.Groups[2].Value = "/"
oMatch.Groups[3].Value = "20"

What is happening here?

I would have expected the 1st one to work which I assume says look for a
pattern of: 2 digits, a slash, 2 more digits, another slass and 4 digits
which is in this string.

Not sure why it found the slashes and the 20 in the last set up.

Thanks,

Tom
 
T

tshad

Actually, it was this post.

I also tried:

oMatch = Regex.Match(valueIn,
@"([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})");
strValue = oMatch.Groups[1].Value;

and got:
oMatch.Groups[1].Value = "02"
oMatch.Groups[2].Value = "23"
oMatch.Groups[3].Value = "2007"

Closer but not correct.

Thanks,

Tom
tshad said:
I have a text string that has a date in it and I am trying to pull out the
date. In my earlier posts I got a couple of patterns that seem correct,
but I can't seem to get them to work. I want to account for 1 or 2 digits
in the month and day fields and 2 or 4 in the year field.

After not getting the correct result I put it down to the simplest pattern
which would match exactly the number of digits and I get nothing.

For example, if I have the following string:

valueIn = "At 02/23/2007 DOM";

I want to end up with 02/23/2007.

If I use:

Match oMatch;
oMatch = Regex.Match(valueIn, @"\d{2}/\d{2}/\d{4}");

I get nothing from my oMatch.Groups[1].Value.

If I have:

oMatch = Regex.Match(valueIn, @"\d{1,2}/\d{1,2}/\d{2,4}");
strValue = oMatch.Groups[1].Value;

I get nothing from my oMatch.Groups[1].Value;

If I have:

oMatch = Regex.Match(valueIn,
@"\d{1,2}(-|/)\d{1,2}(-|/)(\d{2}|\d{4})");
strValue = oMatch.Groups[1].Value;

oMatch.Groups[1].Value = "/"
oMatch.Groups[2].Value = "/"
oMatch.Groups[3].Value = "20"

What is happening here?

I would have expected the 1st one to work which I assume says look for a
pattern of: 2 digits, a slash, 2 more digits, another slass and 4 digits
which is in this string.

Not sure why it found the slashes and the 20 in the last set up.

Thanks,

Tom
 
J

Jesse Houwing

Hello tshad,
I have a text string that has a date in it and I am trying to pull out
the date. In my earlier posts I got a couple of patterns that seem
correct, but I can't seem to get them to work. I want to account for
1 or 2 digits in the month and day fields and 2 or 4 in the year
field.

After not getting the correct result I put it down to the simplest
pattern which would match exactly the number of digits and I get
nothing.

For example, if I have the following string:

valueIn = "At 02/23/2007 DOM";

I want to end up with 02/23/2007.

If I use:

Match oMatch;
oMatch = Regex.Match(valueIn, @"\d{2}/\d{2}/\d{4}");
I get nothing from my oMatch.Groups[1].Value.

If I have:

oMatch = Regex.Match(valueIn, @"\d{1,2}/\d{1,2}/\d{2,4}");
strValue = oMatch.Groups[1].Value;
I get nothing from my oMatch.Groups[1].Value;

If I have:

oMatch = Regex.Match(valueIn,
@"\d{1,2}(-|/)\d{1,2}(-|/)(\d{2}|\d{4})");
strValue = oMatch.Groups[1].Value;
oMatch.Groups[1].Value = "/"
oMatch.Groups[2].Value = "/"
oMatch.Groups[3].Value = "20"
What is happening here?

I would have expected the 1st one to work which I assume says look for
a pattern of: 2 digits, a slash, 2 more digits, another slass and 4
digits which is in this string.

Not sure why it found the slashes and the 20 in the last set up.

Tom, this is due to the fact that () create groups. So if your regex contains
() it will automatically create grouped values in your match object.

In your case there is no need to use groups at all, so you can change your
code to:

if (oMatch.Success)
{
string unparsedDate = oMatch.Value;
}

Alternatively, if you want to use groups, or want to learn more about groups:

use this regex
@"(\d{1,2})[-/](\d{1,2})[-/](\d{2}|\d{4})";

Now oMatch.Groups[1].Value will contain the month
Now oMatch.Groups[2].Value will contain the day
Now oMatch.Groups[3].Value will contain the year

Also note that I changed (-|/) to [-/], so that it doesn't give me more groups
than I actually need...
 
J

Josh Einstein

In addition, I also find it useful to use named groups because it's easy to
inadvertently create anonymous groups if you tweak the regex and that would
break your code. The following is Jesse's regex using named groups.

@"(?<m>\d{1,2})[-/](?<d>\d{1,2})[-/](?<y>\d{2}|\d{4})";

Now you can use Match.Groups["m"].Value // month

Josh Einstein

Jesse Houwing said:
Hello tshad,
I have a text string that has a date in it and I am trying to pull out
the date. In my earlier posts I got a couple of patterns that seem
correct, but I can't seem to get them to work. I want to account for
1 or 2 digits in the month and day fields and 2 or 4 in the year
field.

After not getting the correct result I put it down to the simplest
pattern which would match exactly the number of digits and I get
nothing.

For example, if I have the following string:

valueIn = "At 02/23/2007 DOM";

I want to end up with 02/23/2007.

If I use:

Match oMatch;
oMatch = Regex.Match(valueIn, @"\d{2}/\d{2}/\d{4}");
I get nothing from my oMatch.Groups[1].Value.

If I have:

oMatch = Regex.Match(valueIn, @"\d{1,2}/\d{1,2}/\d{2,4}");
strValue = oMatch.Groups[1].Value;
I get nothing from my oMatch.Groups[1].Value;

If I have:

oMatch = Regex.Match(valueIn,
@"\d{1,2}(-|/)\d{1,2}(-|/)(\d{2}|\d{4})");
strValue = oMatch.Groups[1].Value;
oMatch.Groups[1].Value = "/"
oMatch.Groups[2].Value = "/"
oMatch.Groups[3].Value = "20"
What is happening here?

I would have expected the 1st one to work which I assume says look for
a pattern of: 2 digits, a slash, 2 more digits, another slass and 4
digits which is in this string.

Not sure why it found the slashes and the 20 in the last set up.

Tom, this is due to the fact that () create groups. So if your regex
contains () it will automatically create grouped values in your match
object.

In your case there is no need to use groups at all, so you can change your
code to:

if (oMatch.Success)
{
string unparsedDate = oMatch.Value;
}

Alternatively, if you want to use groups, or want to learn more about
groups:

use this regex
@"(\d{1,2})[-/](\d{1,2})[-/](\d{2}|\d{4})";

Now oMatch.Groups[1].Value will contain the month
Now oMatch.Groups[2].Value will contain the day
Now oMatch.Groups[3].Value will contain the year

Also note that I changed (-|/) to [-/], so that it doesn't give me more
groups than I actually need...
 
T

tshad

Jesse Houwing said:
Hello tshad,
I have a text string that has a date in it and I am trying to pull out
the date. In my earlier posts I got a couple of patterns that seem
correct, but I can't seem to get them to work. I want to account for
1 or 2 digits in the month and day fields and 2 or 4 in the year
field.

After not getting the correct result I put it down to the simplest
pattern which would match exactly the number of digits and I get
nothing.

For example, if I have the following string:

valueIn = "At 02/23/2007 DOM";

I want to end up with 02/23/2007.

If I use:

Match oMatch;
oMatch = Regex.Match(valueIn, @"\d{2}/\d{2}/\d{4}");
I get nothing from my oMatch.Groups[1].Value.

If I have:

oMatch = Regex.Match(valueIn, @"\d{1,2}/\d{1,2}/\d{2,4}");
strValue = oMatch.Groups[1].Value;
I get nothing from my oMatch.Groups[1].Value;

If I have:

oMatch = Regex.Match(valueIn,
@"\d{1,2}(-|/)\d{1,2}(-|/)(\d{2}|\d{4})");
strValue = oMatch.Groups[1].Value;
oMatch.Groups[1].Value = "/"
oMatch.Groups[2].Value = "/"
oMatch.Groups[3].Value = "20"
What is happening here?

I would have expected the 1st one to work which I assume says look for
a pattern of: 2 digits, a slash, 2 more digits, another slass and 4
digits which is in this string.

Not sure why it found the slashes and the 20 in the last set up.

Tom, this is due to the fact that () create groups. So if your regex
contains () it will automatically create grouped values in your match
object.

In your case there is no need to use groups at all, so you can change your
code to:

if (oMatch.Success)
{
string unparsedDate = oMatch.Value;
}

Exactly what I need.
Alternatively, if you want to use groups, or want to learn more about
groups:

use this regex
@"(\d{1,2})[-/](\d{1,2})[-/](\d{2}|\d{4})";

Now oMatch.Groups[1].Value will contain the month
Now oMatch.Groups[2].Value will contain the day
Now oMatch.Groups[3].Value will contain the year

Also note that I changed (-|/) to [-/], so that it doesn't give me more
groups than I actually need...

Didn't know that the () was creating the groups.

Thanks,

Tom
 
T

tshad

Josh Einstein said:
In addition, I also find it useful to use named groups because it's easy
to inadvertently create anonymous groups if you tweak the regex and that
would break your code. The following is Jesse's regex using named groups.

@"(?<m>\d{1,2})[-/](?<d>\d{1,2})[-/](?<y>\d{2}|\d{4})";

Now you can use Match.Groups["m"].Value // month

Does make it clear.

Thanks,

Tom
Josh Einstein

Jesse Houwing said:
Hello tshad,
I have a text string that has a date in it and I am trying to pull out
the date. In my earlier posts I got a couple of patterns that seem
correct, but I can't seem to get them to work. I want to account for
1 or 2 digits in the month and day fields and 2 or 4 in the year
field.

After not getting the correct result I put it down to the simplest
pattern which would match exactly the number of digits and I get
nothing.

For example, if I have the following string:

valueIn = "At 02/23/2007 DOM";

I want to end up with 02/23/2007.

If I use:

Match oMatch;
oMatch = Regex.Match(valueIn, @"\d{2}/\d{2}/\d{4}");
I get nothing from my oMatch.Groups[1].Value.

If I have:

oMatch = Regex.Match(valueIn, @"\d{1,2}/\d{1,2}/\d{2,4}");
strValue = oMatch.Groups[1].Value;
I get nothing from my oMatch.Groups[1].Value;

If I have:

oMatch = Regex.Match(valueIn,
@"\d{1,2}(-|/)\d{1,2}(-|/)(\d{2}|\d{4})");
strValue = oMatch.Groups[1].Value;
oMatch.Groups[1].Value = "/"
oMatch.Groups[2].Value = "/"
oMatch.Groups[3].Value = "20"
What is happening here?

I would have expected the 1st one to work which I assume says look for
a pattern of: 2 digits, a slash, 2 more digits, another slass and 4
digits which is in this string.

Not sure why it found the slashes and the 20 in the last set up.

Tom, this is due to the fact that () create groups. So if your regex
contains () it will automatically create grouped values in your match
object.

In your case there is no need to use groups at all, so you can change
your code to:

if (oMatch.Success)
{
string unparsedDate = oMatch.Value;
}

Alternatively, if you want to use groups, or want to learn more about
groups:

use this regex
@"(\d{1,2})[-/](\d{1,2})[-/](\d{2}|\d{4})";

Now oMatch.Groups[1].Value will contain the month
Now oMatch.Groups[2].Value will contain the day
Now oMatch.Groups[3].Value will contain the year

Also note that I changed (-|/) to [-/], so that it doesn't give me more
groups than I actually need...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top