我尝试检查输入字符串是否为整数,但是没有任何东西返回我创建的模式的正结果。
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
string input = "1";
string int_num = "^0$|^[1-9][0-9]*$";
regex pattern(int_num, regex_constants::grep);
if(regex_match(input, pattern))
cout << "matched\n";
else
cout << "not matched\n";
return 0;
}
答案 0 :(得分:2)
这对我有用,虽然根据ECMAScript正则表达式语法你的版本似乎也是正确的。
"^(0|[1-9][0-9]*)$"
答案 1 :(得分:2)
您的模式不起作用的原因是因为您已指定正则表达式应模拟grep
的语法。
如果从正则表达式定义中删除该标志,则您的模式可以正常工作:
string int_num = "^0$|^[1-9][0-9]*$";
regex pattern(int_num);
以下是修改过的脚本示例:https://ideone.com/iETnsw
如果必须指定正则表达式语法,请使用ECMAScript
,而不是grep
。
答案 2 :(得分:2)
First of all, regex pattern should be changed to "^(0|[1-9][0-9]*)$"
because it looks better. Then, change regex_constants::grep
to regex_constants::ECMAScript
, or leave blank, because ECMAScript is default. At the end, it is required to compile with g++-4.9, because g++-4.8 version doesn't work with regex lib.
答案 3 :(得分:0)
使用Regex101.com,您可以判断您的表达式与数字不匹配。您可以使用表达式\d+
来匹配数字。
另一种验证整数的方法是做这样的事情:
int y;
cin >> y;
if (cin.fail()) {
//Is not an integer
}
答案 4 :(得分:0)
我认为最好的方法是遍历每个字符以检查它们是否为数字,如果它到达结尾,那么它就是一个数字。也许会有所帮助。虽然没有测试过。
<?php if($aForm['sTaskType'] !== 'CP' ){?>
<table style="width: 95%">
<tr>
<th>Area</th>
<th>Pass</th>
<th>Pass with feedback</th>
<th>Fail with Minors</th>
<th>Fail with Majors</th>
</tr>
<?php foreach ($aQualityTeamResults AS $iBusinessStreamId => $aTeamData) {
$aQualityAgentResults = $oRadiusQualityFns->GetQualityAgentResults($sDateFrom, $sDateTo, $sTaskType, $aTeamData['iBusinessStreamId']);?>
<tbody>
<tr class="TeamClick<?php echo $iBusinessStreamId;?>">
<td><?php echo $aTeamData['sBusinessStream']?></td>
<td><?php echo $aTeamData['Pass']?></td>
<td><?php echo $aTeamData['Pass with Feedback']?></td>
<td><?php echo $aTeamData['Fail with Minors']?></td>
<td><?php echo $aTeamData['Fail with Majors']?></td>
</tr>
</tbody>
<?php foreach ($aQualityAgentResults AS $iUserId => $aAgentData) {
$aQualityPropertyResults = $oRadiusQualityFns->GetQualityPropertyResults($sDateFrom, $sDateTo, $sTaskType, $aAgentData['iBusinessStreamId'], $aAgentData['Agent']);
?>
<tbody>
<tr class="Agent<?php echo $iUserId; ?>">
<td><?php echo $oRadiusUser->Get_User_Name($aAgentData['Agent']);?></td>
<td><?php echo $aAgentData['Pass'];?></td>
<td><?php echo $aAgentData['Pass with Feedback'];?></td>
<td><?php echo $aAgentData['Fail with Minors'];?></td>
<td><?php echo $aAgentData['Fail with Majors'];?></td>
</tr>
</tbody>
<?php foreach ($aQualityPropertyResults AS $iUserId2 => $aPropertyData) { ?>
<tbody>
<tr class="Property<?php echo $iUserId2 ;?>">
<td colspan="2"><a href="<?echo $sPage?>?sPostPropertyCode=<?php echo $aPropertyData['sPropertyCode'];?>&sPostTaskType=<?php echo $aPropertyData['sTaskType'];?>&iPostUserId=<?php echo $iUserId;?>"><font color="black"><?php echo $aPropertyData['sPropertyCode']?></font></a></td>
<td colspan="3"><?php echo $aPropertyData['Result']?></td>
</tr>
</tbody>
<?php
}
}
}
?>
</table>
答案 5 :(得分:0)
您必须将语法规范删除为grep,然后才能正常工作。
int main() { std :: string input =“1”;
std::regex pattern ("^0$|^[1-9][0-9]*$");
if(regex_match(input, pattern))
cout << "matched\n";
else
cout << "not matched\n";
return 0;
}
输出:匹配