weixin_39636245
2020-12-27 04:33 阅读 1

how can we get

we can test and replace but how can we get the match part from the source string

该提问来源于开源项目:VerbalExpressions/CppVerbalExpressions

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

6条回答 默认 最新

  • weixin_39816062 weixin_39816062 2020-12-27 04:33

    Apologies for late reply, didn't see this until now.

    Can you be more specific? What are you trying to do?

    点赞 评论 复制链接分享
  • weixin_39636245 weixin_39636245 2020-12-27 04:33
    RegularExpression re("([0-9]+) ([0-9]+)");
    std::string s = "123 456";
    assert (re.subst(s, "$2-$1") == 1);
    assert (s == "456-123");
    

    like this,$1, $2 we can get the match subpart form the source string, so we can use it

    and another problem how can we use match "\"

    点赞 评论 复制链接分享
  • weixin_39816062 weixin_39816062 2020-12-27 04:33

    This doesn't support what you want to do, however you can do the same thing using the veregex namespace which will use either std or boost based on your config. Look at the bool test(const std::string & value) function for how you could do so. If you think of a nice api to use I'd be happy to include it in this library. Maybe there could be an std::vector<std::string> extract(const std::string & value) function added?

    For matching "\" you need to double escape like so:

    
        verex expr = verex()          
                    .search_one_line()
                    .start_of_line()
                    .then( "\\\\" )
                    .end_of_line();
    
        std::cout << (expr.test("\\") ? "matches" : "doesn't match") << std::endl;
    
    点赞 评论 复制链接分享
  • weixin_39636245 weixin_39636245 2020-12-27 04:33

    thank you very much.

    and

    RegularExpression re("([0-9]+) ([0-9]+)"); std::string s = "123 456"; assert (re.subst(s, "$2-$1") == 1); assert (s == "456-123");

    code like this,it's from Poco which implement with lot Dependency. So I can't add it to your library easily.

    点赞 评论 复制链接分享
  • weixin_39816062 weixin_39816062 2020-12-27 04:33

    Sure, but same thing could be achieved with the standard..

    I do really like your suggestion, this should support what you want to do. If I come up with api change I'll ping you :+1:

    点赞 评论 复制链接分享
  • weixin_39636245 weixin_39636245 2020-12-27 04:33

    en. just ping(do) me(it)!

    点赞 评论 复制链接分享

相关推荐