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