none
Using RegEx to parse string RRS feed

  • Question

  • Dear all,

    I am not use to RegEx for parsing string but I thing in my scenario that is a good candidate for extracting result from follwing string:

    String 1:

    {"savingsGoal":"123,123.00","alreadySaved":"12,321.00","fortnightlyDeposit":"5,000.00","goalDate":"","interestRate":"4.00","rwtRate":"33","resultMessageKey":"resultTerm","result1":"10","result2":""}}

    From the string above how to use regEx to return all string starting by Result* ?

    String2{"homeLoanBalance":"500,000.00","loanLength":"12","interestRate":"5.9","combinedAccountBalances":"50,000","monthlySavings":"12,321.00","savingsResult":22619.6,"timeSavedResult":"2
    years, 10 months"}}

    For the string 2, how can I retrive results : monthlySavings":"12,321.00","savingsResult":22619.6,"timeSavedResult":"2
    years, 10 months"}} ?

    Thnaks for help
    regards

    serge


    Your knowledge is enhanced by that of others.

    • Moved by Bob Shen Tuesday, March 26, 2013 4:35 AM move to a more appropriate forum
    Monday, March 25, 2013 8:57 AM

Answers

  • Hi Serge,

    As you described:

    Result : Value
    Result1 : Value
    Result2: Value

    It looks like you need to split them string into key-value pairs.

    If so, how about just split it with ","(include the double quotes)?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 26, 2013 7:22 AM
    Moderator

All replies

  • Hello,

    String1:

    MatchCollection allMatchResults = null;
    Regex regexObj = new Regex("^Result");
    allMatchResults
    = regexObj.Matches(subjectString);

     foreach (Match m in allMatchResults )
     
    {
          
    Console.WriteLine(m.Value);
     
    }

    String2:

    MatchCollection allMatchResults = null;
    Regex regexObj = new Regex(""":""(.*)"",");
    allMatchResults
    = regexObj.Matches(subjectString);

     foreach (Match m in allMatchResults )
     
    {
          
    Console.WriteLine(m.Value);
     
    }


    João Sousa (MCTS) Senior Software Engineer

    • Proposed as answer by puni_vidhya Monday, March 25, 2013 10:01 AM
    Monday, March 25, 2013 9:31 AM
  • Hello thnaks for your reply.

    It does not work, it cannot find any for both samples .

    Any idea ?


    Your knowledge is enhanced by that of others.

    Monday, March 25, 2013 10:02 AM
  • If you want to extract a part of your strings beginning with a specified word you could do with some pattern replacement, like the following:

    static void CatchStr1(string str) {
        string pattern = "^.*(resultM.*).*";
    
        string result = Regex.Replace(str, pattern, "$1");
        Console.WriteLine("{0}", result);
    
    }
    
    static void CatchStr2(string str) {
        string pattern = "^.*(monthlySa.*).*";
    
        string result = Regex.Replace(str, pattern, "$1");
        Console.WriteLine("{0}", result);
    }
    
    

    Kind regards,

    wizend

    Monday, March 25, 2013 10:06 AM
  • yes but I need to get the different results in an array.

    For a single result string I was using  :

    Regex r = new Regex(@"result.*?\:(\w+)");
    string SavingResult = r.Match(response).Groups[1].Value;

    Then in value, I get directly the result. I try to do the same thing with string 1 and 2 I give you above.

    Any tips?

    regards

    serge


    Your knowledge is enhanced by that of others.

    Monday, March 25, 2013 10:27 AM
  • I'm afraid, I don't quite understand. What do you mean by getting the different results in an array? Could you show an example. By the way your pattern shown above does not work with your String1, does it?

    wizend

    Monday, March 25, 2013 11:20 AM
  • The pattern I describe is working with follwoing reques result :

    {"calculateMortgageRewards":{"loanAmount":"500,000.00","loanLength":"25","interestRate":"5.25","rewardScheme":"flybuys","result":1008.0}}

    But the issue is that if tere is decimal, it will not collect them

    What I try to achive is to get the result value of each result string return by the request as :

    Result : Value
    Result1 : Value
    Result2: Value

    Some return result have a single result but some other more so I try to build up different simple way to extract those results string + their values

    regards

    serge


    Your knowledge is enhanced by that of others.

    Monday, March 25, 2013 11:29 AM
  • Hi Serge,

    As you described:

    Result : Value
    Result1 : Value
    Result2: Value

    It looks like you need to split them string into key-value pairs.

    If so, how about just split it with ","(include the double quotes)?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 26, 2013 7:22 AM
    Moderator