Regex. C GNU Extract Filter data

Source: Internet
Author: User

Today was @svchao interest to come. There is a regular expression of interest picked up, try to notepad++ the basic grammar is not forgotten, but if it is used in embedded solutions, it seems to be a bit laborious.

First mark a basic grammar.

Single character match

In square brackets, for example, [0135678] means 0 1 3 5 6 7 8 All can pass this match

Use square brackets, for example [^azaz] to indicate that all except a Z A and z can be matched by that byte

Allow-represents a range, such as [0-9a-za-z] to denote all numbers and letters

Commonly used single characters have a common expression, such as \w means [0-9a-za-z] the rest of the slowly added

A description of the number of matches using {}, for example \w{2} means that two consecutive characters are numbers or letters

Use () to capture comparison results ...

Above content self-science.

In order to adapt to the use of the scene for the embedded found this demo

Slre (Github Super light Regular Expression Library)

But I do not seem to support {}, and did not find greed-related ... So hold on for a moment

And then said to familiarize myself with the GNU Regex.h.

Find that the Regex interface with Python is much like the reason for POSIX.

Pit Dad's virtual machine has not been updated for a long time, tossing a while after the environment is finally testing the effect.

#include <stdio.h>#include<sys/types.h>#include<regex.h>intMainintChar_c,Char**Char_v) {    Char* P_str ="Url = www.google.com.hk;"; Char* P_reg ="{0,} (\\s+) {0,}= {0,} (\\s+) {0};";    regex_t reg; regmatch_t matchs[ -]; memset (Matchs,0,sizeof(matchs)); intR; R= Regcomp (&Reg, P_reg, reg_extended); R= Regexec (&reg, P_str, -, Matchs,0);
    return 0 ;}

Because it is their own test code, so did not specifically to do the output, the direct breakpoint hit down to see the results,

Where reg_extended must add, or some features can not be used,

Another strange point is that matchs[0] will have a matching range for the entire p_reg, so if we only need to extract the target results from our definition (),

Starting from p_reg[1] is good.

Spit Groove C string syntax, tune for a long time ... The result is a pot with \ \ ... But there seems to be no more efficient solution.

The next step, if used again, may be to manually add a greedy part to the slre, or find the GNU regex.c to be cropped ...

I'll talk.

#include <stdio.h>#include<sys/types.h>#include<regex.h>#include<string.h>intMainintChar_c,Char**Char_v) {    Char* P_str ="Url = www.google.com.hk;"; Char* P_reg ="{0,} (\\s+) {0,}= {0,} (\\s+) {0};";    regex_t reg; regmatch_t matchs[ -]; intR, I; R= Regcomp (&Reg, P_reg, reg_extended); if(r! =0) {printf ("Err"); } R= Regexec (&reg, P_str, -, Matchs,0); if(r! =0) {printf ("Err"); }     for(i =0; I < -; i++) {        intStart_index =Matchs[i].rm_so; intEnd_index =Matchs[i].rm_eo; intLen = End_index-Start_index; if(Start_index >=0) {            Chardis_buffer[ the]; strncpy (Dis_buffer,&P_str[start_index], Len); Dis_buffer[len]=' /'; printf ("catch:%s\n", Dis_buffer); }    }    return 0;}

Since I grabbed the output for a long time ... I decided to post the data capture code ...

Use environment for g++ with ECLIPSE-CDT

Debug is used to not change the IDE

Catch:  Url        =  www.google.com.hk; catch:Urlcatch:www.google.com.hk

The test output is correct ...

Regex. C GNU Extract Filter data

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.