[Mono-list] Regex question

Jaroslaw Kowalski jaak@zd.com.pl
Wed, 18 Feb 2004 12:39:02 +0100


I think that Carlos wants to ignore the @ inside quotes. Here's the proposed
solution:

using System;
using System.Text.RegularExpressions;

class C1
{
    public static void Main()
    {
        string regex = @"('.*?\@.*?')*[^'*]*?(?<param>@\w+)";
        string textToMatch = "update users set PhotoPath =
'aaa@doma@@in.com' where userid = @aaa or id=@bbb";

        Regex r = new Regex(regex);
        MatchCollection m = r.Matches(textToMatch);

        Console.WriteLine(m.Count);
        for (int i = 0; i < m.Count; ++i)
        {
            Console.WriteLine("match {0} is {1}", i,
m[i].Groups["param"].Value);
        }
    }
}

It prints:

2
match 0 is @aaa
match 1 is @bbb

The regex consists of 3 parts:

first part (optional) matches a quote followed by a sequence of characters
including @ and terminated by a quote. This is used to kill quoted @.
the second part matches any sequence of characters other than a quote or
"at" sign
the third part matches @ followed by a word.

Hope it helps.

Jarek

----- Original Message ----- 
From: "Chris Turchin" <chris@turchin.net>
To: "Carlos Guzmán Álvarez" <carlosga@telefonica.net>
Cc: "mono-list" <mono-list@lists.ximian.com>
Sent: Wednesday, February 18, 2004 11:56 AM
Subject: Re: [Mono-list] Regex question


> then try it with \W* instead, that way you limit on nonword characters,
but onyl
> if they are there...
>
>
> Regex regex = new Regex(@"
> \W*(@\w+)\W*
>
> ",
> (RegexOptions) 0);
>
> --chris
>
> On Wed, 18 Feb 2004, Carlos Guzmán Álvarez wrote:
>
> > Hello:
> >
> > > How about something like:
> > >
> > > \W+(@\w+)\W+
> >
> > Thanks but seems that it's not valid.
> >
> >
> >
> >
> > --
> > Best regards
> >
> > Carlos Guzmán Álvarez
> > Vigo-Spain
> > _______________________________________________
> > Mono-list maillist  -  Mono-list@lists.ximian.com
> > http://lists.ximian.com/mailman/listinfo/mono-list
> >
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>