파일 스트림에서 입력을 구문 분석하려고하고 여러 단어의 가능성을 다루므로 일반 지원과 완벽하게 사용자 정의 할 수있는 경우 - 즉, 입력 유형이 이면 Regular Expressions이 필요합니다.
gcc의 순간에는 C++ 11의 정규식을 사용할 수 있지만 그 때는 isn't supported입니다.
그래서, 하나 개의 솔루션은 ++ 03과 C c를 표준 C++ 98 작동합니다 부스트 C++ 라이브러리를 사용하는 것입니다 + +0 :
#include <string>
#include <iostream>
#include <cstdlib>
#include <boost/regex.hpp>
using namespace std;
int main() {
string text = "hss cscf \"serving\" 32.5 ims 112.134";
boost::regex e("(\\w+)\\s(\\w+)\\s\"(\\w+\\s?)+\"\\s([0-9]+(\\.[0-9][0-9]?)?)\\s(\\w+)\\s([0-9]+(\\.[0-9][0-9]?)?)");
boost::sregex_token_iterator iter(text.begin(), text.end(), e, 0);
boost::sregex_token_iterator end;
for(; iter != end; ++iter) {
std::cout << *iter << std::endl;
}
return 0;
}
당신은 (GCC를 사용하여 컴파일 할 수 있습니다 나는 gcc-4.7을 사용했다.2)를 통해 다음과 같은 : 당신이 전체 decimal parsing using a regex를 지원하기 위해, 소원 경우 horridly 긴 정규식, 다음 위의 다음과 같은 문자열이 제대로 작동 이유에 관해서는
g++ {filename} -std={language version} -I{your boost install location} -L{your boost library location} -o {output filename} {your boost library location}/libboost_regex.a
:
"hss cscf \"serving\" 32.5 ims 112.134"
"hss cscf \"serving more than one\" 32.5 ims 112.134"
"hss cscf \"serving\" 32 ims 112"
참고 :
부스트 Regex : http://www.solarix.ru/for_developers/api/regex-en.html
좋아, 내가 선을 읽고 다음 char에 의해 char 검색. 그것의 '' '', 나는 그것의 다중 단어를 안다.하지만 정수 나 부동에 관해서는 붙어있다. char에 대해 if (line [i]> = 'a'&& line [i] <= 'z ')하지만 integer 또는 float이 다음 값일 때 어떻게 진행할 것인가? – Jigyasa