search

Home  >  Q&A  >  body text

正则表达式 - c++标准库中没有关于正则匹配字符串的函数么?

我想实现的是查找满足正则条件的字符串,google了一下,发现都是用的boost中的函数,难道标准库中没有相关函数么?

巴扎黑巴扎黑2819 days ago627

reply all(6)I'll reply

  • 迷茫

    迷茫2017-04-17 11:04:55

    Under Linux, you can easily use the library provided by regex.h. I will post a piece of code first to show how RE is used in C language
    , more roughly

    #include<stdio.h>
    #include<sys/types.h>
    #include<regex.h>
    #include<memory.h>
    #include<stdlib.h>
    
    int main()
    {
     
        char *bematch = "hhhericchd@gmail.com";
        char *pattern = "h{3,10}(.*)@.{5}.(.*)";
        char errbuf[1024];
        char match[100];
        
        regex_t reg;
        int err,nm = 10;
        regmatch_t pmatch[nm];
     
        if(regcomp(&reg,pattern,REG_EXTENDED) < 0){
            regerror(err,&reg,errbuf,sizeof(errbuf));
            printf("err:%s\n",errbuf);
        }
     
        err = regexec(&reg,bematch,nm,pmatch,0);
        if(err == REG_NOMATCH){
            printf("no match\n");
            exit(-1);
        }else if(err){
            regerror(err,&reg,errbuf,sizeof(errbuf));
            printf("err:%s\n",errbuf);
            exit(-1);
        }
     
        for(int i=0;i<10 && pmatch[i].rm_so!=-1;i++){
            int len = pmatch[i].rm_eo-pmatch[i].rm_so;
            if(len){
                memset(match,'
    "h{3,10}(.*)@.{5}.(.*)"
    ',sizeof(match)); memcpy(match,bematch+pmatch[i].rm_so,len); printf("%s\n",match); } } return 0; }

    I want to see if an email address matches the pattern I provided. This email address is
    hhhericchd@gmail.com pattern is

    rrreee

    reply
    0
  • 阿神

    阿神2017-04-17 11:04:55

    • There is definitely no regular library in C 98
    • C 0x has std::regex, currently only VS2010 supports it and GCC (libstd ) does not support it
    • PCRE/PCRE is a relatively old C/C regular library, cross-platform
    • There is a regular library in glibc under Linux, directly include "regex.h"
    • You can use com to adjust the IRegExp2 regular interface of vbscript under windows. It is applicable to any version of windows. It does not require any additional dependencies and is very fast
    • There are also regular expressions in boost, but the boost library has too many things and is quite bloated

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 11:04:55

    You can use the regex library that comes with Linux.

    reply
    0
  • 阿神

    阿神2017-04-17 11:04:55

    It seems that there are only some third-party ones, Microsoft has one, and the boost one should be more versatile.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 11:04:55

    pcre is a famous regular library. PHP uses it

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 11:04:55

    c 11<regex>
    Eg:

    #include <regex>
    #include <iostream>
    using namespace std;
    //匹配手机号码
    int main() {
        regex reg;
        regex_constants::syntax_option_type fl = regex_constants::icase;
        reg.assign("\b1[35][0-9]\d{8}|147\d{8}|1[8][01236789]\d{8}\b", fl);
        bool found1 = regex_match("13536921192", reg);
        cout<<found1<<endl;
    }

    See: http://www.codeceo.com/article/cpp11-regex-code.html

    reply
    0
  • Cancelreply