none
Extracting a value from a string

    Question

  • Hello,

    Can some one please tell me an easy way by which I can extract a value from this staring..What I am looking for is the word sale which I have made bold  but it can be any word in that place..but the format of this whole string will always be the same.

    Lists%2fLights%2fsale&FolderCTID=&View=%7bA7FB17B0%2dDCB1%2d4453%2dA6E9%2d6A729929DA73%7d.

    Thanks

    mardi 1 mai 2012 19:13

Réponses

  • You could create your regex then grab third third segment into a group, like this:

                Regex r = new Regex(@"\w*%2f\w*%2f(\w*)&FolderCTID=.*");
     
                var result = r.Match(input);
     
                if (result.Success)
                {
                    Console.WriteLine(result.Groups[1].Value);
                }

    The parenthesis around the third (\w*) means you want to capture that part of the match into a group, which will be stored in Group[1] if the match is good (Group[0]) contains the whole match.


    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...

    mardi 1 mai 2012 20:14

Toutes les réponses

  • Is it always in the same index position?  I guess my question is really what do you mean by "the format of this whole string will always be the same"?

    That is, is it just that %2f is the delimiter and it's always the 3rd element?  Or that it always starts with exactly Lists%2fLights%2f


    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...


    mardi 1 mai 2012 19:14
  • It always starts with exactly Lists%2fLights%2f..what I meant was that say this is our string

    Lists%2fLights%2fsale&FolderCTID=&View=%7bA7FB17B0%2dDCB1%2d4453%2dA6E9%2d6A729929DA73%7d.

    in this the sale word can be replace by anything like gadget,csharp or anything..I need to get that word ..Let me know if I need to clarify more.

    Thanks for the quick reply.

    mardi 1 mai 2012 19:18
  • Well, a really easy way, providing the format stays exactly the same, is to just substring it:

    // just making consts for ease of use

    const string header = "Lists%2fLights%2f";             const string after = "&FolderCTID";

    // find start of after part, beginning after header then extract             var endPos = input.IndexOf(after, header.Length);             var word = input.Substring(header.Length, endPos - header.Length);

                  

    Though, of course, you could also do a RegEx as well...


    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...


    mardi 1 mai 2012 19:23
  • That's working great ..Thank you ..but just in case if the other words are changing is it easy to do with regex like in the string

    Lists%2fLights%2fsale&FolderCTID=&View=%7bA7FB17B0%2dDCB1%2d4453%2dA6E9%2d6A729929DA73%7d.  if the second word lights is different then how can we use Regex to extract the word sale.

    Thanks

    mardi 1 mai 2012 19:51
  • That would depend on what parts of the input string are truly fixed.  That is, do you see this string as always being in the form:

    {something}%2f{something}%2f{target}&FolderCTID=&View={something}


    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...

    mardi 1 mai 2012 19:54
  • That is correct.
    mardi 1 mai 2012 20:01
  • You could create your regex then grab third third segment into a group, like this:

                Regex r = new Regex(@"\w*%2f\w*%2f(\w*)&FolderCTID=.*");
     
                var result = r.Match(input);
     
                if (result.Success)
                {
                    Console.WriteLine(result.Groups[1].Value);
                }

    The parenthesis around the third (\w*) means you want to capture that part of the match into a group, which will be stored in Group[1] if the match is good (Group[0]) contains the whole match.


    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...

    mardi 1 mai 2012 20:14
  • Beautiful..Thank you so much :)
    mardi 1 mai 2012 20:17