我很想知道如何检查是否字符串有两个坐标的格式,如:检查是否字符串格式“坐标/坐标”
(signed int x,signed int y)
我已经找到通过搜索一些答案但我还没有完全得到它们(刚开始用C++),我正在寻求一个简单的解决方案或提示如何检查这一点。谢谢!
我很想知道如何检查是否字符串有两个坐标的格式,如:检查是否字符串格式“坐标/坐标”
(signed int x,signed int y)
我已经找到通过搜索一些答案但我还没有完全得到它们(刚开始用C++),我正在寻求一个简单的解决方案或提示如何检查这一点。谢谢!
我会用这一个(简单一些可能存在):
^\(\-{0,1}\d*,\-{0,1}\d*\)
那就是:
^\( start by a parenthesis
\-{0,1} 0 or 1 "-"
\d* any digit
, ","
和重复。
会工作吗?: 'if(string ==“\ d *,\ d *”)' – 2015-03-19 14:09:52
@Yíu请阅读本文[关于C++正则表达式](http://www.cplusplus.com/reference/regex/regex_match /) – 2015-03-19 14:11:55
好的,感谢Link @Thomas – 2015-03-19 14:12:36
我假设你需要特别采取一个字符串作为输入。我会检查字符串的每个值。
string str;
// something happens to str, to make it a coordinate
int n = 0;
int m = 48;
bool isANumber;
bool hasASlash = false;
while ((n < str.length()) and isANumber) {
isANumber = false;
if (str.at(n) == '/') {
hasASlash = true; // this means there is a slash somewhere in it
}
while ((m <= 57) and !isANumber) {
// makes sure the character is a number or slash
if ((str.at(n) == m) or (str.at(n) == '/')) isANumber = true;
m++;
}
m = 48;
n++;
}
if (hasASlash and isANumber) {
// the string is in the right format
}
请纠正我,如果我做错了什么......
你有没有听说过正则表达式? – 2015-03-19 13:12:16
你可能想要正则表达式。看看一些教程,网上有很多。 – vektor 2015-03-19 13:12:26
关于正则表达式,[请先阅读本文](http://programmers.stackexchange.com/questions/223634/what-is-meant-by-now-you-have-two-problems)。如果你决定正则表达式仍然是你的问题的解决方案(很可能是,不要完全忽视它),那么阅读[C++中的正则表达式支持](http://en.cppreference.com/瓦特/ CPP /正则表达式)。 – 2015-03-19 13:16:26