GUN C Regular Expression

Source: Internet
Author: User
Tags expression posix regular expression

The text is processed in the most recent project, so you use the gun regular expression, which is POSIX-style. We usually use Perl style, so it might be a bit of a habit to start with. The detailed distinction can be seen on the wiki:

Http://en.wikipedia.org/wiki/Regular_expression

The header file is the regex.h that can see the interface he provides. Here are 3 functions and one structure:

Reference

int regcomp (regex_t *compiled, const char *pattern, int cflags)
int regexec (regex_t *compiled, char *string, size_t nmatch, regmatch_t matchptr [], int eflags)
void regfree (regex_t *compiled)
typedef struct {
  regoff_t rm_so;
  regoff_t rm_eo;
} regmatch_t;

Regcomp compiles the parameter pattern to compiled, which is to say that the string is compiled as a regular expression.

and the parameter cflags can be the following 1 or several combinations:

reg_extended

Use the extended POSIX Regular Expressions.

Reg_icase

Ignore case

Reg_nosub

does not store matching results, only returns whether the match was successful.

Reg_newline

You can match line wrapping.

Regexec performs a match. Compiled is the regular expression that was just compiled, string is the string to be matched, and nmatch is the length of the subsequent structure array (regmatch_t). Matchptr is an array of regmatch_t ( Which is where the $0,$1, like Perl, are stored. The Eflag parameter can be 1 or more of the following.

Reg_notbol

Will say ^ as a generic character match, not the beginning of a line

Reg_noteol

Will say $ as a generic character match, not end of line

RegFree you need to release the regular expression after each run. Compiled is a regular expression that needs to be freed.

The Rm_so in regmatch_t is the starting position of the matching character, and the Rm_eo is the ending position.

Say so much, actually use very simple:

Reference

POSIX Regexp Compilation: Using regcomp to prepare to match.
Flags for POSIX Regexps: Syntax variations for regcomp.
Matching POSIX Regexps: Using regexec to match the compiled pattern that you get from regcomp.
Regexp Subexpressions: Finding which parts of the string were matched.
Subexpression Complications: Find points of which parts were matched.
Regexp Cleanup: Freeing storage; reporting errors.

Related Article

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.