Skip to content

Instantly share code, notes, and snippets.

@brokendish
Created June 24, 2017 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brokendish/73174b46500f0d7006e28a68d2b09bf4 to your computer and use it in GitHub Desktop.
Save brokendish/73174b46500f0d7006e28a68d2b09bf4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <boost/regex.hpp>
#include <locale>
using namespace std;
int alphaNumericCheck(string a);
int numberOfDigit(string str, int max);
int mbSplitChar2Vector(const char* str, vector<string>& ret);
//
//g++ -o Hello HelloWorld.cpp -lboost_regex && ./Hello
//
int main(){
setlocale( LC_CTYPE, "ja_JP.UTF-8");
//setlocale( LC_CTYPE, "ja_JP.EUC-JP");
cout << "-------------半角英数字のみチェック---------------" << endl;
//半角英数字のみチェック
alphaNumericCheck("aaaa");
alphaNumericCheck("aaaa1");
alphaNumericCheck("aaaaAA");
alphaNumericCheck("aaaa!");
alphaNumericCheck("aaaaア");
cout << "-------------桁数チェック(文字列、最大桁数)---------------" << endl;
//桁数チェック(文字列、最大桁数)
numberOfDigit("12345", 5);
numberOfDigit("1 2 3 4 5 ", 11);
numberOfDigit("abc",2);
numberOfDigit("あいう", 9);
cout << "-------------字列を1文字単位でVectorに格納する---------------" << endl;
vector<string> ret;
//半角カナ存在チェック
const string aa1="def";
const char* aaa1 = aa1.c_str();
mbSplitChar2Vector(aaa1, ret);
cout << "-------------------" << endl;
const string aa2="ア";
const char* aaa2 = aa2.c_str();
mbSplitChar2Vector(aaa2, ret);
cout << "-------------------" << endl;
const string aa3="アブ";
const char* aaa3 = aa3.c_str();
mbSplitChar2Vector(aaa3, ret);
cout << "-------------------" << endl;
const string aa4="ア";
const char* aaa4 = aa4.c_str();
mbSplitChar2Vector(aaa4, ret);
cout << "-------------------" << endl;
for (int i=0;i<ret.size();i++){
cout << "#" << ret[i] << "#" << endl;
}
cout << "-------------半角カナ存在チェック---------------" << endl;
//半角カナ存在チェック
return 0;
}
//----------------------------------------------------
//文字列を1文字単位でVectorに格納する。
//(半角カナでも全角文字でも半角英数でもmblenで判定して1文字ちぎる)
//----------------------------------------------------
int mbSplitChar2Vector(const char* str, vector<string>& ret){
char *rep;
int i=0;
while(str[i] != '\0'){
char *rep;
int len = mblen(&str[i],MB_CUR_MAX);
cout << "MBLEN=" << (int)len << endl;
rep = (char *)malloc(sizeof(char) * strlen(str) );
/*ワークを初期化*/
for(int j=0 ; j<len + 1 ; j++){
rep[j]='\0';
}
/*文字数を指定してコピー*/
strncpy(rep,str + i,len);
cout << "MOJI=" << rep << endl;
ret.push_back(rep);
free(rep);
i+=len;
}
}
//----------------------------------------------------
//半角英数字のみチェック
//----------------------------------------------------
int alphaNumericCheck(string str){
//半角英数字のみ-----------------------------Start
boost::regex reg("^[0-9A-Za-z]+$");
boost::smatch mat;
if( boost::regex_search(str,mat,reg)){
cout << "全て半角英数字" << endl;
}
else{
cout << "半角英数字出ないものあり!" << endl;
}
//半角英数字のみ-----------------------------End
}
//----------------------------------------------------
//桁数チェック
//----------------------------------------------------
int numberOfDigit(string str,int max){
cout << "文字 : " << str << " | サイズ : " << str.size() << endl;
cout << "文字 : " << str << " | サイズ : " << str.length() << endl;
if(max < str.size()){
cout << "桁数オーバー" << endl;
}
else{
cout << "桁数OK!" << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment