Re: Pattern Match



On Sep 7, 12:58 pm, "konrad Krupa" <kon...@xxxxxxxxxxxxxxx> wrote:
Thank you for the tip.

The pattern you suggested fixes problem 12345 6789

but I still need to get match on string that has only 123 45

Is there any way to get them in one pattern?

Konrad."Doug Semler" <dougsem...@xxxxxxxxx> wrote in message

news:1189179369.874384.278270@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx



On Sep 7, 11:33 am, Doug Semler <dougsem...@xxxxxxxxx> wrote:
On Sep 7, 11:16 am, "konrad Krupa" <kon...@xxxxxxxxxxxxxxx> wrote:

I'm not expert in Pattern Matching and it would take me a while to come
up
with the syntax for what I'm trying to do.
I hope there are some experts that can help me.

I'm trying to match /d/d/d/s/d/d in any text.

There could be spaces in front or after the pattern (the nnn nn could
be
without spaces also) but it shouldn't pick it up in case like this

1234 56768

above pattern would give me 234 56.

If I do this /s/d/d/d/s/d/d/s
then I have to have spaces in front and after it, which is not the
case.

Konrad.

howbout something like
[^\d]\d{3}\s+\d{2}

(Non digit followed by 3 digits followed by one or more whitespace
followed by 2 digits)

P.S. If you want to get only the digit match, you'll want to capture
it by sticking it into a catpuring group, like this:

[^\d](\d{3}\s+\d{2})- Hide quoted text -

- Show quoted text -

\d{3}\s+\d{2} should do the trick. There should be 2 matches each
with 1 captured group in each match. For example

given input "123 45" matches[0].Groups[0].Captures[0].Value will be
"123 45"
given input "123 45 abcd 12345 6789and then some" then there will be 2
matches:
- matches[0].Groups[0].Captures[0].Value = "123 45"
- matches[1].Groups[0].Captures[0].Value = "345 67"

Since there are no parenthesis only the default captured sub-
expression (which is the entire match) matches.

Here's a demo program I use to test out regular expressions. Compile
it then call it passing a a regular expression (surround by double
quotes if the expression has spaces) and a string to test (again
double quote it if it contains spaces).

static void Main(string[] args)
{
string pat = args[0];
string text = args[1];

Console.WriteLine("pattern: {0}\ninput: {1}", pat, text);

Regex r = new Regex(pat);
MatchCollection matches = r.Matches(text);

Console.WriteLine("number of matches: {0}", matches.Count);
if ( matches.Count > 0 )
{
for (int i=0; i < matches.Count; i++)
{
Console.WriteLine("captured groups in match[{0}]: {1}", i,
matches[i].Groups.Count);
for (int j=0; j < matches[i].Groups.Count; j++)
{
Console.WriteLine("substrings captured by group[{0}]: {1}", j,
matches[i].Groups[j].Captures.Count);
for (int k=0; k < matches[i].Groups[j].Captures.Count; k++)
{
Console.WriteLine("captured substring[{0}]={1}", k,
matches[i].Groups[j].Captures[k].Value);
}
}
}
}






.