V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xuboying
V2EX  ›  程序员

喜欢正则和 python 的 C++er 来看下

  •  
  •   xuboying · 2016-03-08 14:01:55 +08:00 · 2649 次点击
    这是一个创建于 2971 天前的主题,其中的信息可能已经有所发展或是发生改变。

    平时主要从事日志、数据分析工作,早些年接触了 Perl, 对其自带的正则表达式爱不释手,后来赶时髦也学习了 python , c++,每隔一两个月更新一下自己写的 C++工具。发现 C++ 没有很好的正则库( STL 的太弱),就引入了 PCRE,但 PCRE 的接口调用麻烦,文档太少。先是随意分装了自己一个 C++版本,几个月不用居然忘记用法了,现在决定按照 Python 的语法,封装 PCRE2,这样,每次要用的时候只要参考 python 的文档就行了

    库设计的很简单,用 auto 和智能指针,使得语法和 python 基本近似,对宽字符的处理也做了很好的处理,有完整的文档和实例,如果喜欢这个项目,请给个小小的 star 。谢谢

    https://github.com/xuboying/PCRE2Plus

    13 条回复    2016-03-16 08:06:32 +08:00
    msg7086
        1
    msg7086  
       2016-03-08 14:41:59 +08:00   ❤️ 1
    如果对 Perl 的正则爱不释手的话,不试试 Ruby 吗(拖走
    xuboying
        2
    xuboying  
    OP
       2016-03-08 14:54:37 +08:00
    @msg7086 使用何种语言往往不是 coder 决定的啦
    TangMonk
        3
    TangMonk  
       2016-03-08 15:01:43 +08:00   ❤️ 1
    Owenjia
        4
    Owenjia  
       2016-03-08 15:08:18 +08:00 via Android   ❤️ 1
    boost 的 regex 不好用么?
    xuboying
        5
    xuboying  
    OP
       2016-03-08 15:12:38 +08:00
    @Owenjia boost 的没有用过唉,不过我只想记忆一套正则,就是 Perl/PCRE 的正则,这个标准做的最好, python 的也和它兼容,语法上, python 的文档写的非常好,初学者一看就懂。
    skydiver
        6
    skydiver  
       2016-03-08 15:17:48 +08:00   ❤️ 1
    好像发错节点了,应该发到分享创造
    alexapollo
        7
    alexapollo  
       2016-03-08 15:24:42 +08:00   ❤️ 1
    looks good
    Owenjia
        8
    Owenjia  
       2016-03-08 20:21:24 +08:00 via Android
    @xuboying
    没记错的话 boost_regex 默认是 PCRE 的吧?
    xuboying
        9
    xuboying  
    OP
       2016-03-08 20:58:54 +08:00 via Android   ❤️ 1
    @Owenjia 好像是。这个项目目的是多个选择吧,我每个项目都会考虑用正则,但不会都想用 boost 。用 Python 的语法也是为了简化 port Python prototype 到 c++的步骤。还有 pcre 貌似不支持替换,这是我后来换 pcre2 的原因。
    patrickstar
        10
    patrickstar  
       2016-03-09 09:12:58 +08:00
    不错,有空来测试一下子,很想有个单纯的 C++的正则库,装个 boost 来仅用正则式,感觉好像为了吃点猪肉而来杀一头猪一样
    patrickstar
        11
    patrickstar  
       2016-03-15 16:06:44 +08:00
    @xuboying 请教一下子,如何在 pcre2_substitute() 函数中使用 后向引用 啊?

    比如我想把"cat in the the hat"中重复的 the 去掉一个, pattern = "(\b[a-z]+) \1", replacement = "\1"

    结果一执行,把两个 the 替换为 \1 了,好像它不对 replacement 参数做引用解释!

    谢谢
    xuboying
        12
    xuboying  
    OP
       2016-03-15 19:05:41 +08:00   ❤️ 1
    @patrickstar 这个我在 soc 文档里提到了,你要用 $1 来替换\1 ,
    我用了 PCRE 文档里的 PCRE2_SUBSTITUTE_EXTENDED ,但似乎还是不能用\1


    e.sub(pattern, repl, string, count=0, flags=0)

    Supported

    static std::string re::sub(const std::string & pattern, const std::string & repl, const std::string & Str, size_t count = 0, int flags = 0)

    static std::wstring re::sub(const std::wstring & pattern, const std::wstring & repl, const std::wstring & Str, size_t count = 0, int flags = 0)

    Error Handling: pattern only

    NOTE: \x e.g(\1) as group reference in repl text is not support, use $x e.g $1 instead
    patrickstar
        13
    patrickstar  
       2016-03-16 08:06:32 +08:00
    @xuboying 试了一下子,没看出 PCRE2_SUBSTITUTE_EXTENDED option 有啥子作用,但是对于 pcre2_substitute() 采用 \$1 来取代 \1 进行后向引用是可行的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5401 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:02 · PVG 15:02 · LAX 00:02 · JFK 03:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.