0

String matching in C++ can't get the correct result, but in java can Java code is as follows

       String txt = "  static char k_a123[] =  \"fefeffefefwefwef/fefe/f/ef/ef\\0\"; int fefefefefeeffef = 123;     static char k_a123233[] =  \"fef/efef/efef\\0\";     int fwefnewff=21323;     static char k_a1233334[] =  \"<init>\\0\";     static char k_a123555[] =  \"fefefef)fefe/fef/V\\0\";     addAll(123,333);";
       String reg = "\\sstatic\\schar\\sk_\\S+\\s=";
       Pattern p = Pattern.compile(reg, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
       Matcher m = p.matcher(txt);
       while (m.find()) {
           for (int i = 0; i <= m.groupCount(); i++) {
               System.out.println(" ===== " + m.group(i));
           }
       }
// fowllow is outpu result
// =====  static char k_a123[] =
// =====  static char k_a123233[] =
// =====  static char k_a1233334[] =
// =====  static char k_a123555[] =

C++ code is as follows

    std::string s1 = "  static char k_a123[] =  \"fefeffefefwefwef/fefe/f/ef/ef\\0\"; int fefefefefeeffef = 123;     static char k_a123233[] =  \"fef/efef/efef\\0\";     int fwefnewff=21323;     static char k_a1233334[] =  \"<init>\\0\";     static char k_a123555[] =  \"fefefef)fefe/fef/V\\0\";     addAll(123,333);";

    std::string reee = "\\sstatic\\schar\\sk_\\S+\\s="; // Double Quote String 4
    std::string xxx(reee);

    std::regex rgx(xxx);

    std::smatch match;
    std::regex_search(s1, match, rgx);
    for (uint32_t index = 0; index < match.size(); index++)
    {
        printf("bbb [%d]  %s\n", index, std::string(match[index]).c_str());
    }
// xcode output
// bbbb [0]   static char k_v1[] =
// bbbb [1]  
/********* xcode output over ,nothing...... why?   *******************/

I think it's because c++ truncates the string using \0, and java matches the correct result. C++ uses xcode to run code What can I do to get the same result for java and c++?

These 2 parts of the code can be copied and run directly, but the java output is correct. C++ is incorrect. How to make them output the same result.

xxking
  • 77
  • 10
  • Your match loops are iterating once too often in both cases. The condition should be `<`, not `<=`. I don't know why you're adding nulls to these strings. Java doesn't need them, and C++ already supplies them. – user207421 Apr 15 '19 at 12:16
  • 2
    Possible duplicate of [How to match multiple results using std::regex](https://stackoverflow.com/questions/21667295/how-to-match-multiple-results-using-stdregex) – Dan M. Apr 15 '19 at 12:23
  • 1
    _"I think it's because"_ -- why conjecture when you can test? Do a test run after removing the `\0` characters from your input string. – JaMiT Apr 15 '19 at 15:35

1 Answers1

0

thanks for :Dan M.

follow is answer

How to match multiple results using std::regex

 std::string s1 = "  static char k_a123[] =  \"fefeffefefwefwef/fefe/f/ef/ef\\0\"; int fefefefefeeffef = 123;     static char k_a123233[] =  \"fef/efef/efef\\0\";     int fwefnewff=21323;     static char k_a1233334[] =  \"<init>\\0\";     static char k_a123555[] =  \"fefefef)fefe/fef/V\\0\";     addAll(123,333);";

    std::string rrr = "\\sstatic\\schar\\sk_\\S+\\s=\\s(.*?);";
    std::regex r(rrr);
    const std::sregex_token_iterator end;
    for (std::sregex_token_iterator i(s1.cbegin(), s1.cend(), r, 1); i != end; ++i) {
        cout <<"\n ----- " << *i << endl;
    }
    cout << "====" << endl;


  [1]: http://how-to-match-multiple-results-using-stdregex
xxking
  • 77
  • 10