为什么我的代码不匹配这些 s 表达式的正则表达式?
Why is my code not matching regexes for these s-expressions?
我正在尝试采用包含不同变量的 S 表达式并根据它们的类型对它们进行标记。我对正则表达式很陌生,所以我不完全确定为什么这只匹配括号和变量类型的 else 条件。如果您知道为什么我的正则表达式与标记不匹配,请告诉我!
#include <string>
#include <regex>
#include <iostream>
#define print(var) std::cout << var << std::endl
std::string INT_REGEX = "\b[0-9]{1,3}[0-9]{1,3}\b",
DOUBLE_REGEX = "\b[0-9]{1,3}.[0-9]{1,3}\b",
BOOLEAN_REGEX = "^(true|false)$";
bool matchRegex(std::string pattern, std::string inputString) {
std::regex expression(pattern);
return std::regex_match(inputString, expression);
}
void detectTokenType(std::string strToken) {
if (strToken == "(" | strToken == ")")
print("Parenthesis");
else if (matchRegex(INT_REGEX, strToken))
print("Integer");
else if (matchRegex(DOUBLE_REGEX, strToken))
print("Double");
else if (matchRegex(DOUBLE_REGEX, strToken))
print("Boolean");
else
print("Variable name or string");
}
void tokenize(std::string listData) {
std::vector<char> tokenBuffer;
for (int i = 0; i < listData.length(); i++) {
char currChar = listData[i];
if (i == listData.length() - 1) {
tokenBuffer.push_back(currChar);
std::string strToken(tokenBuffer.begin(), tokenBuffer.end());
detectTokenType(strToken);
}
else if (currChar != ' ') {
tokenBuffer.push_back(currChar);
}
else {
std::string strToken(tokenBuffer.begin(), tokenBuffer.end());
tokenBuffer.clear();
detectTokenType(strToken);
}
}
}
int main() {
std::string codeSnippet = "( 2 3.0 true )";
tokenize(codeSnippet);
return 0;
}
在您的正则表达式字符串中,您使用的 \b
不是单词边界。相反,您需要 \b
。同样,.
有特殊含义(它是一个匹配任何字符的通配符)。如果要匹配文字 .
,则需要 \.
.
此外,您正在检查 INT_REGEX
中至少 2 位数字,这是不必要的:
std::string INT_REGEX = "\b[0-9]{1,3}\b",
DOUBLE_REGEX = "\b[0-9]{1,3}\.[0-9]{1,3}\b",
BOOLEAN_REGEX = "^(true|false)$";
此外,您也在检查 DOUBLE_REGEX
的 Boolean
情况,因此您需要修复它。
这是 demo.
我正在尝试采用包含不同变量的 S 表达式并根据它们的类型对它们进行标记。我对正则表达式很陌生,所以我不完全确定为什么这只匹配括号和变量类型的 else 条件。如果您知道为什么我的正则表达式与标记不匹配,请告诉我!
#include <string>
#include <regex>
#include <iostream>
#define print(var) std::cout << var << std::endl
std::string INT_REGEX = "\b[0-9]{1,3}[0-9]{1,3}\b",
DOUBLE_REGEX = "\b[0-9]{1,3}.[0-9]{1,3}\b",
BOOLEAN_REGEX = "^(true|false)$";
bool matchRegex(std::string pattern, std::string inputString) {
std::regex expression(pattern);
return std::regex_match(inputString, expression);
}
void detectTokenType(std::string strToken) {
if (strToken == "(" | strToken == ")")
print("Parenthesis");
else if (matchRegex(INT_REGEX, strToken))
print("Integer");
else if (matchRegex(DOUBLE_REGEX, strToken))
print("Double");
else if (matchRegex(DOUBLE_REGEX, strToken))
print("Boolean");
else
print("Variable name or string");
}
void tokenize(std::string listData) {
std::vector<char> tokenBuffer;
for (int i = 0; i < listData.length(); i++) {
char currChar = listData[i];
if (i == listData.length() - 1) {
tokenBuffer.push_back(currChar);
std::string strToken(tokenBuffer.begin(), tokenBuffer.end());
detectTokenType(strToken);
}
else if (currChar != ' ') {
tokenBuffer.push_back(currChar);
}
else {
std::string strToken(tokenBuffer.begin(), tokenBuffer.end());
tokenBuffer.clear();
detectTokenType(strToken);
}
}
}
int main() {
std::string codeSnippet = "( 2 3.0 true )";
tokenize(codeSnippet);
return 0;
}
在您的正则表达式字符串中,您使用的 \b
不是单词边界。相反,您需要 \b
。同样,.
有特殊含义(它是一个匹配任何字符的通配符)。如果要匹配文字 .
,则需要 \.
.
此外,您正在检查 INT_REGEX
中至少 2 位数字,这是不必要的:
std::string INT_REGEX = "\b[0-9]{1,3}\b",
DOUBLE_REGEX = "\b[0-9]{1,3}\.[0-9]{1,3}\b",
BOOLEAN_REGEX = "^(true|false)$";
此外,您也在检查 DOUBLE_REGEX
的 Boolean
情况,因此您需要修复它。
这是 demo.