The regex that comes with Linux

Source: Internet
Author: User
Tags preg

You can use regex.h directly to support regular expressions under Linux.

Android also has this header file, which can be thought of as supported by Android.

#include <sys/types.h>
#include <regex.h>

int Regcomp (regex_t *preg, const char *regex, int cflags);

int regexec (const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags);

size_t regerror (int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size);

void RegFree (regex_t *preg);

The Rm_so is the match string start offset (start offset), and the rm_eo is the terminating offset of the matching string (end offset).

typedef struct {
regoff_t Rm_so;
regoff_t Rm_eo;
} regmatch_t;

1#include <stdio.h>2#include <regex.h>3#include <string.h>4 5 intMainvoid)6 {7     Const Char*str ="aabbccdd.com";8     Const Char*pattern ="^(.+?) \\.com$";9 regex_t reg;Tenregmatch_t match[Ten]; One  A     intRET =0; -ret = Regcomp (&reg, pattern, reg_extended |reg_newline); -     if(Ret! =0) theprintf"error\n"); -     Else -     { -ret = regexec (&reg, str,Ten, Match,0); +         if(Ret! =Reg_nomatch) -         { +             intLen = match[1].rm_eo-match[1].rm_so; A             Charbuf[1024x768] = {0}; atmemcpy (buf, str + match[1].rm_so, Len); -printf"final BUF%s\n", buf); -         } -     } -RegFree (&reg); -     return 0; in}

The regmatch_t array is used to store matching results, the reason for the array is actually to match the group, its rules and Java and other implementations are consistent, if the match succeeds, the array of [0] is the entire matching string, that is, group (0), the other for each match to the group.

Man regex

The regex that comes with Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.