C ++ practice: Regular Expressions parse sound card parameters and regular expressions sound card

Source: Internet
Author: User

C ++ practice: Regular Expressions parse sound card parameters and regular expressions sound card

C ++ practice: using regular expressions to parse sound card Parameters

The delimiter used by the regular expression in Java. It's hard to swallow it. C ++ 11 to parse sound card parameters. [Txt2re is quite useful] [1].

// ================================================ ==============================================

// Name: CppPro. cpp

// Author:

// Version:

// Copyright: Your copyright notice

// Description: Hello World in C ++, Ansi-style

// ================================================ ==============================================

# Include

# Include

# Include

Using namespace std;

/**

* In the constructor

*/

Class Card {

Public:

Int getId () const {

Return id;

}

Private:

Int id;

String name;

Class Device {

Int id;

};

};

String trim (string & str)

{

Size_t first = str. find_first_not_of ('');

If (first = string: npos)

Return "";

Size_t last = str. find_last_not_of ('');

Return str. substr (first, (last-first + 1 ));

}

/**

* Start a Loop to receive client commands

*/

Int main (){

Std: string id;

Std: string name;

Smatch sm; // container for storing string results

Std: string txt = "0 [PCH]: HDA-Intel-HDA Intel PCH ";

Std: string re1 = "()"; // Any Single Character 1

Std: string re2 = "(\ d +)"; // Integer Number 1

Std: string re3 = "()"; // Any Single Character 2

Std: string re4 = "(\\[.*? \]) "; // Square Braces 1

Std: string re5 = "(:)"; // Any Single Character 3

Std: string re6 = "()"; // Any Single Character 4

If (regex_search (txt, sm, regex (re1 + re2 + re3 + re4 + re5 + re6 ))){

Id = sm [2];

Name = sm [4];

Name = name. substr (1, name. length ()-2 );

Name = trim (name );

}

Cout <"name:" <name <"id:" <id <endl;

}

Running result:

Name: PCH id: 0

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.