将正则表达式模式从 Javascript 转换为 PCRE (perl) [英] Convert Regular Expression pattern from Javascript to PCRE (perl)

查看:49
本文介绍了将正则表达式模式从 Javascript 转换为 PCRE (perl)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 javascript 正则表达式模式:

This is my javascript regex pattern:

    url = "http://www.amazon.com/gp";    
    hostname = /^((\\w+):\\/\\/\\/?)?((\\w+):?(\\w+)?@)?([^\\/\\?:]+):?(\\d+)?(\\/?[^\\?#;\\|]+)?([;\\|])?([^\\?#]+)?\\??([^#]+)?#?(\\w*)/.exec(url) || [];
// would return "www.amazon.com"

  • 上面的正则表达式从给定的 url 中提取主机名.我需要这条线使用 pcre (c++) 工作.如您所见,我已经为每个 '\' 添加了另一个 '\',但它仍然不起作用.
  • 我需要做哪些额外的更改才能使其在 pcre 代码而不是 javascript 中工作?或者也许这是不可能的,我需要构建全新的模式才能使其在 pcre 中工作?

    what are the additional changes I need to do to make it work in pcre code instead of javascript? or maybe it isn't possible and I need to build entirely new pattern to make it work in pcre?

    这是我的代码的简单版本:

    this is a simple version of my code:

    int main(void)
    {
        string text = "http://www.amazon.com";
        string hostname;
        pcrecpp::RE re("^((\\w+):\\/\\/\\/?)?((\\w+):?(\\w+)?@)?([^\\/\\?:]+):?(\\d+)?(\\/?[^\\?#;\\|]+)?([;\\|])?([^\\?#]+)?\\??([^#]+)?#?(\\w*)");
        if(re.PartialMatch(text, &hostname)) 
        {
            std::cout << "match: " << hostname << "\n";
        }else{
            std::cout << "no match. \n";
        }       
        return 0;
    }
    

    谢谢.

    推荐答案

    没有必要转换它,你唯一需要注意的是转义和/分隔符.

    There's no need to convert it, the only thing you have to take care of is the escaping and the / delimiter.

    请注意,这里可能不是您想要使用的正则表达式.或者至少......不是这样直接的.有很多 url 解析库更适合这个任务.HTParse 例如.

    Do note that a regular expression might not be what you want to use here. Or atleast... not like this directly. There are lots of url parsing libraries that are a lot better suited for this task. HTParse for example.

    您的 C++ 代码应该可以工作,但您的正则表达式有很多可选组,因此很难确定主机名最终会出现在哪个组中.

    Your C++ code should work but your regex has a lot of optional groups so it's hard to be sure in what group the hostname will end up.

    尽管可能很笨拙,但我的编辑适用于此输入

    As hacky as it may be, my edit works for this input

    string text = "http://www.amazon.com";
    string tmp;
    string hostname;
    pcrecpp::RE re("^((\\w+):\\/\\/\\/?)?((\\w+):?(\\w+)?@)?([^\\/\\?:]+):?(\\d+)?(\\/?[^\\?#;\\|]+)?([;\\|])?([^\\?#]+)?\\??([^#]+)?#?(\\w*)");
    if(re.PartialMatch(text, &tmp, &tmp, &tmp, &tmp, &tmp, &hostname))
    {
        std::cout << "match: " << hostname << "\n";
    }else{
        std::cout << "no match. \n";
    }
    

    这篇关于将正则表达式模式从 Javascript 转换为 PCRE (perl)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆