1

I am using the following to match a line. A Line could somthing like below. For instance if field1 is present but the value is null, it doesnt match. I want it to match even if field1 or etc has blank or no value. Any ideas? thanks

   field33
   field1 
   field2   lkjk
   field3   12.01.12

 static string partPattern = @"^(?<Key>\w+)\s+(?<Value>.*)$";

 line = line.Trim();  Match m = Regex.Match(line, partPattern);  
if(m.Groups["Key"].Length > 0) { 
 //do something heree 
}

so when it looks at field33, line becomes field33 and the regex conditional statment fails even though key is there...

1
  • When you say "Does not match" or "is not doing"...what exactly are you attempting to do with this regex? Code examples would help. Commented Jun 20, 2012 at 17:01

1 Answer 1

1

In regex patterns, + indicates One or More.

Try using this string instead

@"^(?<Key>\w+)\s+(?<Value>.*)$

The * indicates Any Number including 0.

Update

I tested the following code, and got this output.

        string t1 = "field1   ";
        string t2 = "field2   iopoi";
        string t3 = "field3   12.12.12";
        Regex rTest = new Regex(@"^(?<Key>\w+)\s+(?<Value>.*)$");
        if (rTest.IsMatch(t1))
        {
            MessageBox.Show("T1 match");
            foreach (Match m in rTest.Matches(t1))
                textBox1.Text += String.Format("Key: {0}\tValue: {1}\r\n", m.Groups["Key"].Value, m.Groups["Value"].Value);
        }
        textBox1.Text += "\n\n";
        if (rTest.IsMatch(t2))
        {
            MessageBox.Show("T2 match");
            foreach (Match m in rTest.Matches(t2))
                textBox1.Text += String.Format("Key: {0}\tValue: {1}\r\n", m.Groups["Key"].Value, m.Groups["Value"].Value);
        }
        textBox1.Text += "\n\n";
        if (rTest.IsMatch(t3))
        {
            MessageBox.Show("T3 match");
            foreach (Match m in rTest.Matches(t3))
                textBox1.Text += String.Format("Key: {0}\tValue: {1}\r\n", m.Groups["Key"].Value, m.Groups["Value"].Value);
        }

Output:

Key: field1 Value: 
Key: field2 Value: iopoi
Key: field3 Value: 12.12.12

I also tested this code calling .Trim() on each initial string.

t1 DID NOT MATCH after calling .Trim()

The reason for this, is because .Trim removes all the whitespace following field1, or field33, or whatever, and the regex requires One or More whitespace characters.

New Regex: Try using this instead @"^(?<Key>\w+)\s*(?<Value>.*)$"

Note that now the \s is also followed by a *. Now it should match after using Trim as well.

Sign up to request clarification or add additional context in comments.

1 Comment

In my testing, this regex matched on all 3 lines. Not sure what else to say to that, what are you trying to do that this is not accomplishing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.