Test and use of the regular expression Greta library in vc6

Source: Internet
Author: User

The processing and analysis of text strings has always been an inevitable problem in writing programs. For a long time, I have been avoiding regular expressions. Maybe it's a lazy reason. Generally, when dealing with string processing problems, you write a small program on your own, but the efficiency is really low. If you do more, you will be a little bored-it's all repetitive work. I had nothing to do yesterday, and finally made up my mind to check the regular expression. I am very glad that the harvest is not small.

For more information about regular expressions, I recommend this article, "getting started with regular expressions for 30 minutes". I am such a basic person who looks at and exercises for two hours, therefore, it is strongly recommended. Other details about the regular expression are not mentioned. It does not belong to the content discussed in this article.

It is essential to learn Regular Expressions and practice. Although there are many tools, I decided to write one myself for the convenience of writing programs by myself. Google, there are quite a few regular library, such as boost, catlreg, Greta, and many others. After a rough look, I thought the boost block was too big and had to compile it step by step. The Lazy gave up. catlreg seemed to be unable to be used in vc6, look at Greta, there are 6 files-they are really friendly, so it is.

There are not many documents about Greta on the Internet. I don't know if it is too simple or there are too few people to use. Most of the examples are examples in the Greta library file, which is too simple, no detailed matching, replacement, and splitting functions are available.

The environment tested in this article is the vc6 (SP6) MFC environment. The steps are as follows,

1) directly copy the six Greta source files to the project directory. For convenience, a Greta directory is created;

Here, the source file is used for testing. In fact, you can compile Greta into a library file to use it. We recommend this method.

2) contains the Greta header file;

# Include <string>
# Include "Greta \ regexpr2.h" // This is enough.
Using namespace STD;
Using namespace RegEx; // namespace of the Greta Library

3) Add the header file "stdafx. H" to the Greta CPP file; otherwise, an error is returned;

In addition, if you still encounter 11 or so inexplicable errors, you can set the MFC to the static link mode. At the beginning, I encountered this problem. I did not know why for a long time, later, I changed the static option. But when I wrote this article, I wanted to see what this error was. I 'd like to post it to you and change the MFC back to the shared link. It's okay... those errors cannot be solved (BT ...)

4) after the environment is set up, you can use it below;

Several important objects:

Rpattern-regular expression mode and setting. It is mainly used;

Match_results -- match the result container;

Subst_results -- replace the result container;

Split_results -- split the result container;

This is basically the case. You can refer to the specific usage and code;

//////////////////////////////////

If (nchar = vk_escape)
Cdialog: onok ();
Else if (nchar = vk_f5) // match
{
Updatedata ();
M_strresult = "";
Match_results result;
Regex_flags DW = Global | allbackrefs;
If (m_bcase) DW | = nocase;
If (m_bmulti) DW | = multiline;
If (m_bsingle) DW | = singleline;
//
Double TMS = clock ();
//
Rpattern PAT (lpctstr) m_strreg, DW );
Int igroups = Pat. cgroups ();
Int ncount = 0;
Match_results: backref_type BR = Pat. Match (lpctstr) m_strsource, result );
If (0) // traverse result method 1. Select either of the following methods.
{
Match_results: backref_vector VEC = result. all_backrefs ();
Match_results: backref_vector: iterator ITER;
If (Br. Matched)
{
For (iter = Vec. Begin (); iter! = Vec. End (); ITER ++)
{
Ncount ++;
String STR = (* ITER). STR ();
M_strresult + = Str. c_str ();
M_strresult + = "\ r \ n ----------------------------------------------- \ r \ n ";
}
}
}
If (1) // traverse result method 2
{
If (Br. Matched)
{
For (INT I = 0; I <result. cbackrefs (); I ++)
{
If (I % igroups = 0)
{
Ncount ++;
M_strresult + = result. backref (I). STR (). c_str ();
M_strresult + = "\ r \ n ----------------------------------------------- \ r \ n ";
}
}
}
}
Double tme = clock ();
Cstring strtip;
Strtip. Format (_ T ("RunTime %. 2fms, % d matched;"), double (TME-TMS), ncount );
Getdlgitem (idc_static_tip)-> setwindowtext (strtip );
//
Updatedata (false );
}
Else if (nchar = vk_f6) // replace
{
Updatedata ();
M_strresult = "";
//
Regex_flags DW = Global | allbackrefs;
If (m_bcase) DW | = nocase;
If (m_bmulti) DW | = multiline;
If (m_bsingle) DW | = singleline;
Double TMS = clock ();
//
Rpattern PAT (lpctstr) m_strreg, (lpctstr) m_strsub, DW );
Subst_results subresult;
//
String STR (lpctstr) m_strsource );
Int ncount = Pat. substitute (STR, subresult );
M_strresult = Str. c_str ();
//
Double tme = clock ();
Cstring strtip;
Strtip. Format (_ T ("running time %. 2fms, % d is replaced;"), double (TME-TMS), ncount );
Getdlgitem (idc_static_tip)-> setwindowtext (strtip );
//
Updatedata (false );
}
Else if (nchar = vk_f7) // splits the string
{
Updatedata ();
M_strresult = "";
//
Regex_flags DW = Global | allbackrefs;
If (m_bcase) DW | = nocase;
If (m_bmulti) DW | = multiline;
If (m_bsingle) DW | = singleline;
Double TMS = clock ();
//
Rpattern PAT (lpctstr) m_strreg, DW );
Split_results splitresult;
//
String STR (lpctstr) m_strsource );
Int ncount = Pat. Split (STR, splitresult );
For (INT ni = 0; Ni <ncount; Ni ++)
{
String strsplit = splitresult [Ni];
M_strresult + = strsplit. c_str ();
M_strresult + = "\ r \ n ----------------------------------------------- \ r \ n ";
}
//
Double tme = clock ();
Cstring strtip;
Strtip. Format (_ T ("RunTime %. 2fms, % d matched;"), double (TME-TMS), ncount );
Getdlgitem (idc_static_tip)-> setwindowtext (strtip );
//
Updatedata (false );
}

//// ---------- End -----------////////

The project code is downloaded here and contains the greta2.6.4 source code file;

The above is the preliminary test result, which may be incorrect or inappropriate in use and introduction. You are welcome to discuss more.

--------------------------------------

Ppzhang, giszhang@gmail.com

 

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.