简体   繁体   English

检查字符串是否采用“坐标/坐标”格式

[英]check if string is in format “coordinate/coordinate”

I would love to know how to check if a string has the format of two coordinates, like: 我很想知道如何检查一个字符串是否具有两个坐标的格式,例如:

(signed int x,signed int y)

I've already found some answers via search but I don't quite get them yet (just started with c++) and I'm asking for a simple solution or hint how to check this. 我已经通过搜索找到了一些答案,但是我还没有得到答案(只是从c ++开始),我想寻求一个简单的解决方案或提示如何进行检查。 Thanks! 谢谢!

I would use this one (some simpler might exist): 我会用这个(可能更简单一些):

^\(\-{0,1}\d*,\-{0,1}\d*\)

Which is: 这是:

^\(       start by a parenthesis
\-{0,1}   0 or 1 "-"
\d*       any digit
,         ","

and repeat. 并重复。

I'm assuming you need to take specifically a string as input. 我假设您需要专门输入一个字符串。 I'd check each value of the string. 我会检查字符串的每个值。

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
}

Please correct me if I've done something wrong... 如果我做错了什么,请纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM