none
String Manipulation RRS feed

  • Question

  • Hi, I am trying to extract a code from a string. The string can vary in content and size but I am using Tag words to make the extraction easier. However, I am struggling to nail a particular scenario. Here is the string:

    "({GoldPrice} * 0.376) + {MP.011} + {SilverPrice}"

    What I need to extract is the 011 part of {MP.011}. The keyword will always be "{MP." It's just the code that will change. Also the rest of the expression can change so for example {MP.011} could be at the beginning, end or middle of the string.

    I've got close using the following:

                    int pFrom = code.IndexOf("{MP.") + "{MP.".Length;
                    int pTo = code.LastIndexOf("}");
                    String result = code.Substring(pFrom, pTo - pFrom);

    However, the result is "011} + {SilverPrice" as it is looking for the last occurrence of }, not the next occurrence. This is where I am struggling.

    Any help would be much appreciated.

    Wednesday, December 26, 2018 1:18 PM

Answers

All replies

  • Try this modification:

       int pTo = code.IndexOf("}", pFrom);

    • Marked as answer by DLarner19 Wednesday, December 26, 2018 4:01 PM
    Wednesday, December 26, 2018 1:38 PM
  • This is a good candidate for regular expression. You can write a regular expression and extract the value.

    See the following code as example:

    string input = "({GoldPrice} * 0.376) + {MP.011} + {SilverPrice}";
    string regex = @"^*{MP.(\d+)}";
    var matches = Regex.Matches(input,regex);
    foreach (Match match in matches)
    {
    			
         foreach (Capture capture in match.Captures)
         {
               Console.WriteLine("Index={0}, Value={1}", capture.Index, capture.Value);
         }
    			
         foreach (Group @group in match.Groups)
         {
            Console.WriteLine("Index={0}, Value={1}", @group.Name, @group.Value);
         }
    }

    See the Demo Fiddle as reference

    or a more better one :

    string input = "({GoldPrice} * 0.376) + {MP.011} + {SilverPrice}";
    string regex = @"^*{MP.(\d+?)}";//@"^*{MP.(\d+)}";
    var matches = Regex.Matches(input,regex);
    var match = matches.OfType<Match>().FirstOrDefault();
    if(match !=null)
        Console.WriteLine("Match= {0}", match.Groups.OfType<Group>().Last());

    Fiddle DEMO

    Hope it helps.



    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    • Edited by Ehsan Sajjad Wednesday, December 26, 2018 2:34 PM
    Wednesday, December 26, 2018 2:19 PM
  • Thanks Ehsan for the suggestion. The issue with this is that the code could be in different formats i.e. 011, 011FM, D011 etc.
    Wednesday, December 26, 2018 4:02 PM
  • A regular expression can written for almost anything.  You need to provide a broad base of example strings to try against.  It may require multiple expressions to get what you want.
    Wednesday, December 26, 2018 5:31 PM