tags:

views:

32

answers:

1

Hello All-

Below is code used to search a string where Identity=" " exists and stores that line in a List. I need to add to this search so that it not only picks up Identity=" " but ALSO where FrameworkSiteID=" ". How can I modify the below code to do this?

Many thanks.

List<KeyValuePair<string, string>> IdentityLines = new List<KeyValuePair<string, string>>();

foreach(FileInfo file in Files) 
            { 
                string line = "";
                using(StreamReader sr = new StreamReader(file.FullName)) 
                { 
                        while(!String.IsNullOrEmpty(line = sr.ReadLine())) 
                        {
                            if (line.ToUpper().Contains("IDENTITY="))
                            {
                                string login = reg.Match(line).Groups[0].Value;
                                IdentityLines.Add(new KeyValuePair<string, string>(file.Name, login));
                            }
                            else
                            {
                                IdentityLines.Add(new KeyValuePair<string, string>(file.Name,"NO LOGIN"));
                            }
                        }
                    //More additional code, not included..
+1  A: 

Fixed:

static void TestRegularExpression()
{
    String line = "Some text here, blah blah Identity=\"EDN\\nuckol\" and FRAMEworkSiteID=\"DesotoGeneral\" and other stuff.";
    Match m1 = Regex.Match(line, "((identity)(=)('|\")([a-zA-Z]*)([\\\\]*)([a-zA-Z]*)('|\"))", RegexOptions.IgnoreCase);
    Match m2 = Regex.Match(line, "((frameworkSiteID)(=)('|\")([a-zA-Z]*)('|\"))", RegexOptions.IgnoreCase);

    if (m1.Success && m2.Success)
    {
        //...
        Console.WriteLine("Success!");
        Console.ReadLine();
    }
}

Here's a regular expression tester I like to use. http://gskinner.com/RegExr/

-Matt

mledbetter
Hi Matt-I've tried your exact code (well I just had to change the parentheses in each case because one had too many and the other didn't have enough) but it never enters the If statement. I know the strings it's searching against have those substrings so I'm not sure what the deal is. I'll keep inspecting the code.
Josh
Hey Josh, sorry about that. I'm working with an injured hand so I fat finger all of my code these days. I updated my post to reflect the exact code I used to test this. It worked fine for me. Maybe if you could post the text you need to match, I could test it.
mledbetter
Here is an example of my strings. I'm still not having any luck but I'll keep trying. <Event Timestamp="2010-08-05T07:16:09.7653762-05:00" Level="INFO" Identity="EDN\nuckol"><Message><Properties IP="175.1.118.61" FrameworkSiteID="DesotoGeneral" User="EDN\nuckol" /><ExtentChanged MinX="1461523" MinY="155555" MaxX="211111" MaxY="1233334" /></Message></Event>
Josh
Hi Matt- any luck?
Josh
That string will return false because Identity has a value. Here's your original question:"Below is code used to search a string where Identity=" " exists and stores that line in a List. I need to add to this search so that it not only picks up Identity=" " but ALSO where FrameworkSiteID=" ". How can I modify the below code to do this?"So, here's my question: Do you want identity to be returned whether or not it has a value. When you ask programmers questions, the more detailed and specific the question, the better the answer usually. :-)
mledbetter
Yes I would like Identity to be returned With the value so Identity="EDN\nuckol" needs to get returned as does FrameworkSiteID="DesotoGeneral"
Josh
Okay, I updated the code. This code assumes that Identity has a value of 0 or more alphas + 0 or more '\' + 0 or more alphas.For FrameworkSiteID, I only search for 0 or more alphas.Hope this helps.-Matt
mledbetter
BTW, this code also assumes that your xml is all part of one line.
mledbetter
Thanks Matt- so once I get inside that If Statement how can I add Both values to the List? the Identity="EDN\nuckol" and the value of FrameworkSiteID="DesotoGeneral" When I use my line of code below it just adds the Identity="EDN\nuckol"
Josh
sorry here is what I have inside my If statement. string login = reg.Match(line).Groups[0].Value; IdentityLines.Add(new KeyValuePair<string, string>(file.Name, login));
Josh
I think I got it. Thanks Matt
Josh
No problem. You should be able to just do String.Split() on m1.Value.ToString() no get the key/value pairs.
mledbetter