Thread: Need help with a regular expression, please?

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90

    Question Need help with a regular expression, please?

    I have , "(`password_hash`(255),`email`(255))" and want to get "password_hash,email".

    So far my best guess has been:
    string pattern = @"[`(\(0-9\))]";
    string pkey = Regex.Replace(tokens[offset], pattern, "");

    but there is another case where I have "(`field4`)" and it comes out to be "field" when I need it to be "field4".

    Any help would be appreciated. I'm terrible with regex.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Hmm... well the fields you want are wrapped in quotes so `[^`]*` should match all the fields.

  3. #3
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Quote Originally Posted by whiteflags View Post
    Hmm... well the fields you want are wrapped in quotes so `[^`]*` should match all the fields.
    Thanks! That helped. My solution is fugly, but it works:
    Code:
    string pattern = @"`[^`]*`";
                            MatchCollection pkeys = Regex.Matches("(`field4`(255),`email`(255))", pattern);
                            string pkey = "";
                            bool first = true;
                            StringBuilder sb1 = new StringBuilder();
                            foreach (Match pk in pkeys)
                            {
                                GroupCollection gc = pk.Groups;
                                if (!first) sb1.Append(",");
                                first = false;
                                sb1.Append(gc[0].Value.Replace("`",""));
                            }
                            pkey = sb1.ToString(); //"field4,email"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. regular expression
    By ashaikh432 in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2010, 06:40 AM
  2. Regular Expression
    By stevesmithx in forum C Programming
    Replies: 0
    Last Post: 02-18-2008, 11:00 AM
  3. regular expression
    By jackel7777 in forum C Programming
    Replies: 4
    Last Post: 08-16-2007, 03:02 AM
  4. about regular expression
    By asert in forum C Programming
    Replies: 1
    Last Post: 11-07-2006, 11:53 AM
  5. Regular Expression
    By tintifaxe in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2006, 07:16 AM