Java
// 匹配 (数字)x(数字) 格式的字符串
String mPattern = "(\\d+)(x|X)(\\d+)";
Pattern r = Pattern.compile(mPattern);
Matcher m = r.matcher(chosedFile);
if (m.find()) {
try {
// 得到两个数字的值
int x= Integer.parseInt(m.group(1));
int y= Integer.parseInt(m.group(3));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
C++
regex_t reg;
regmatch_t pmatch[1];
char *pattern = "[0-9]*x[0-9]*";
int rtn = regcomp(®,pattern,REG_ICASE|REG_EXTENDED);
if(rtn != 0)
return false;
rtn = regexec(®,path,1,pmatch,0);
if(rtn != 0)
return false;
int length = pmatch[0].rm_eo- pmatch[0].rm_so;
char* num1 = new char[length];
char* num2 = new char[length];
int start = pmatch[0].rm_so;
char* num = num1;
for(int i=0;i<length;++i) {
if(path[i+start]=='x'||path[i+start]=='X'){
*num = '\0';
num = num2;
continue;
}
*num = path[i+start];
++num;
}
*num = '\0';
int x= atoi(num1);
int y= atoi(num2);
delete num1;
delete num2;