Re: Regex question




"Jesse Houwing" <jesse.houwing@xxxxxxxxxxxxxxxx> wrote in message
news:21effc907d2518cb38a70ded0bb3@xxxxxxxxxxxxxxxxxxxxx
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


--
Jesse Houwing
jesse.houwing at sogeti.nl




.



Relevant Pages

  • Re: Regex question
    ... structure of the date you're trying to extract. ... For example, in Regex you can ... pattern that will ensure a valid date within the range allowed by T-SQL ... valid date from a string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trouble with $key to HASH when Numeric
    ... the end of a string -- in other words, this pattern will always match ... and capture either 5 digits or nothing; 2) the pattern is anchored to ... modifier, which you do not). ...
    (comp.lang.perl)
  • Re: regex
    ... case sensitivity was part of the problem which I fixed with ... In other word I got syntax problem with the month pattern ... >>against the articles and help on regex, I still can't find the mistake I ... >> Public Function regtest(ByVal StringIn As String, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How do I shorten or split this function
    ...    (scan regex string) ... Instead of field-name you mean field-type. ... it doesn't test the string to see if it's of the suitable form for the ... corresponding field of the pattern. ...
    (comp.lang.lisp)
  • Re: Expressing AND, OR, and NOT in a Single Pattern
    ... much better because the esoterica of regex can make the desired ... results hard to figure out and the bugs in the pattern even harder to ... The only thing I'd do differently to these patterns is add an anchor ... long strings, when the pattern was at the end of the string. ...
    (comp.lang.perl.misc)