1

I am using asp.net. I am trying to split the data which is in datatable. I have a code sample like this:

 {      dt=objErrorLoggingDataAccess.GetErrorDetails(errorID);

        string[] stringSeparators = new string[] { "Message" };
        string error = dt.Rows[0]["Message"].ToString();
        string[] test = error.Split(stringSeparators, StringSplitOptions.None);
        string PageName = test[0].ToString();
        PageNameLabel.Text = PageName;

        stringSeparators=new string[] {HttpContext.Current.Request.Url.ToString()};
        error = dt.Rows[0]["Message"].ToString();
        test = error.Split(stringSeparators, StringSplitOptions.None);
        string Message = test[0].ToString();
        MessageLabel.Text = Message;}

in the datatable following data is there:

 {....ID.......Message.......................................................................................................................
 ....1........http://localhost:10489/images/CategoryIcon/images    Message :   File does not exist. UserName: [email protected]
 ....2........http://localhost:10489/images/CategoryIcon/images    Message : File does not exist. UserName: [email protected]}

My problem is: how can I split the Message and store in the label? I want

   {http://localhost:10489/images/CategoryIcon/images}

separately and UserName separately and the message separately. How can I do that? By executing the above code I am able to split

  { http://localhost:10489/images/CategoryIcon/images 
  }

only. How can I split the Message column and store in pageLabel, MessageLabel, UserNamelabel?

2
  • IF at all possible, change the structure of your datatable (and database) to normalize your results. Don't store multiple values in one column. Commented Aug 13, 2013 at 9:38
  • sir I have normalise the data before but the requirement is like this. Commented Aug 13, 2013 at 9:40

1 Answer 1

1

I would use a regular expression in this case. Because only by splitting this string looks a little bit to inflexible to me.

I tested your data example against this quick and dirty RegEx:

(?<id>\d+)\.*(?<url>\w+:\/\/[\w@][\w.:@]+\/?[\w\.?=%&=\-@/$,]*)\s*Message\s*:\s*(?<message>.*)UserName:\s*(?<username>([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3}))

It supports valid URLs and EMail patterns.

Regex regex = new Regex(
      "(?<id>\\d+)\\.*(?<url>\\w+:\\/\\/[\\w@][\\w.:@]+\\/?[\\w\\.?"+
      "=%&=\\-@/$,]*)\\s*Message\\s*:\\s*(?<message>.*)UserName:\\s"+
      "*(?<username>([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1"+
      ",3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|"+
      "[0-9]{1,3}))",
    RegexOptions.IgnoreCase
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );


// Capture the first Match, if any, in the InputText
Match m = regex.Match(InputText);

// Capture all Matches in the InputText
MatchCollection ms = regex.Matches(InputText);

// Test to see if there is a match in the InputText
bool IsMatch = regex.IsMatch(InputText);

// Get the names of all the named capture groups
// I included your fields as groups: id, url, message and username
string[] GroupNames = regex.GetGroupNames();

I don't know how often you need to call this code. Maybe you get in performance troubles if you have too much data. This regex is q&d - please adjust it to your needs.

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

Comments

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.