Boost C # Regular Expression

Source: Internet
Author: User

String:

Void abtr ("quint32 AB ")
{
String S = tr ("sky is blue .");
OBJ. Foo (TR ('Rose is red .'));
OBJ. Foo (TR ('Rose is red ."));
OBJ. Foo (TR ("Mike (Mike Lee) Say/" He is hungury /""))

}

 

Regular Expression :(? <! /W )(? : TR/() ('| ") (. *)/1 (? :/))

 

C ++ boost:

# Include <boost/RegEx. HPP> <br/> # include <iostream> <br/> # include <fstream> <br/> # include <string> <br/> using namespace STD; <br/> using namespace boost; <br/> int main () <br/> {<br/> ifstream ifs ("E:/regex.txt", IOS :: in); <br/> ifs. seekg (0, IOS: End); <br/> int Len = ifs. tellg (); <br/> char * STR = new char [Len + 1]; <br/> memset (STR, 0, Len + 1 ); <br/> ifs. seekg (0); <br/> ifs. read (STR, Len); <br/> cout <s Tr <Endl; <br/> ifs. close (); </P> <p> string S (STR, Len); <br/> cout <S <Endl; </P> <p> RegEx PAT ("(? <! // W )(?: TR // () ('| /")(.*?) // 1 (?: //) "); <Br/> smatch MAT; <br/> string: const_iterator begin = S. begin (); <br/> string: const_iterator end = S. end (); <br/> while (regex_search (begin, end, mat, Pat) <br/>{< br/> ssub_match subexp = mat [2]; <br/> cout <subexp <Endl; <br/> ssub_match wholeexp = mat [0]; <br/> begin = wholeexp. second; <br/>}< br/> system ("pause"); <br/> return 0; <br/>}< br/>

* Greedy match in boost, so is it added later ?, C # is not required

 

The regular expression class designed in boost:

namespace boost{template <class charT, class traits = regex_traits<charT>  >class basic_regex;typedef basic_regex<char>      regex;typedef basic_regex<wchar_t>   wregex;}

Indicates a regular expression.

 

**************

 

Template <class bidirectionaliterator,
Class Allocator = STD: Allocator <sub_match <bidirealialiterator>
Class match_results;

Typedef match_results <const char *> cmatch;
Typedef match_results <const wchar_t *> wcmatch;
Typedef match_results <string: const_iterator> smatch;
Typedef match_results <wstring: const_iterator> wsmatch;

 

Returns a matching result, which contains a group composed of sub_match (the subexpression in parentheses in a regular expression is a group)

[0] is also a sub_match that indicates the substring that the entire regular expression matches. The first and second represent the start and end positions of the entire substring.

[1]... Group

 

************

Template <class bidirectionaliterator>
Class sub_match;

Typedef sub_match <const char *> csub_match;
Typedef sub_match <const wchar_t *> wcsub_match;
Typedef sub_match <STD: String: const_iterator> ssub_match;
Typedef sub_match <STD: wstring: const_iterator> wssub_match;

Represents a group. sub_match inherits from STD: pair. First indicates the starting position of the matched string, and second indicates the Ending position.

Sub_match can be implicitly converted to a string type.

 

**************

Two functions:

Regex_match // complete match. If the entire string to be matched fully matches the regular expression, true is returned.

Regex_search // partial match. Find the matched substring.

 

 

 

C #

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. io; <br/> using system. text. regularexpressions; <br/> namespace regextest <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> streamreader sr = new streamreader (@ "E:/regex.txt"); <br/> string STR = sr. readtoend (); <br/> Sr. close (); <br/> RegEx Reg = New RegEx ("(? <! // W )(?: TR // () ('|/") (. *) // 1 (?: //) "); <Br/> int start = 0; <br/> int end = Str. length; <br/> while (true) <br/>{< br/> match MAT = reg. match (STR, start); <br/> If (mat. success = false) <br/> break; <br/> console. writeline (mat. groups [2]); <br/> Start = mat. index + mat. length; <br/>}< br/> console. read (); <br/>}< br/>

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.