如何在C ++上使用正则表达式匹配以下坐标模式?

时间:2017-10-26 17:19:41

标签: c++ regex string coordinates

我想检查它是否符合以下格式:

(整数,整数),包括括号和逗号。例如:for(3,4)将返回true,而(6.4将返回false 我试过

string input;
regex check("(\\-|+)?[[:d:]]+,?(\\-|+)?[[:d:]]+");
cin >> input;
if (regex_match(input, check)) cout << "okay" << endl;
else cout << "error";

但是我遇到了运行时错误

2 个答案:

答案 0 :(得分:2)

看来你正在寻找

<!DOCTYPE html>
<html>
	<head>
		<title>HighCharts - Pattern Color</title>
		<meta http-equiv="Content-Type" contetn="text/html;" charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">

		<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />

		<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
		<script src="https://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>

		<script src="https://code.highcharts.com/highcharts.js"></script>
		<script src="https://highcharts.github.io/pattern-fill/pattern-fill.js"></script>
		<script src="https://code.highcharts.com/modules/exporting.js"></script>
	</head>

	<body>

		<script type="text/javascript">
			$(document).ready(function() {
			    Highcharts.chart('container', {
				    chart: {
				        type: 'bar',
				        backgroundColor: null
				    },
				    title: {
				        text: 'Como você está?'
				    },
				    xAxis: {
				        categories: ['']
				    },
				    yAxis: {
				        min: 0,
				        title: {
				            text: ''
				        }

				    },
				    legend: {
				        reversed: true
				    },
				    plotOptions: {
				        series: {
				            stacking: 'percent',
				            dataLabels: {
				                enabled: true,
				                format: '<b>{point.percentage:.2f}%</b>',
				                //point.percentage:.1f
				                color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
				            }
				        }
				    },
				    series: [{
				        name: 'Alegre',
				        data: [30],
				        color: {
	          		         pattern: 'http://emoticons.topfriends.biz/favicon.png',
	          		         width: '30',
	          		         height: '30'
	     	 		      }
	         	 		// color: '#B22222'
				    }, {
				    	name: 'Feliz',
				        data: [30],
				          color: {
				                   pattern: 'https://t4.ftcdn.net/jpg/01/58/30/09/240_F_158300957_MhRWEx1vDO6SPVHdGS4dqNG7nLP8rdZ4.jpg',
				                   width: '30',
				                   height: '30'
				           }
		                // color: '#2E8B57'
				    }]
				});
			});
		</script>

		
		<div id="container" style="min-width: 80%; height: 200px; max-width: 80%; margin: 0 auto"></div>
		
	</body>
</html>

当与regex check(R"(\([-+]?\d+,[-+]?\d+\))") 一起使用时,它会定义类似^\([-+]?\d+,[-+]?\d+\)$的模式,需要完整的字符串匹配。

详细说明:

  • std::regex_match - 字符串的开头(隐含在^
  • regex_match - \(
  • ( - 1或0 [-+]?+字符
  • - - 一位或多位
  • \d+ - 逗号
  • , - 1或0 [-+]?+字符
  • - - 一位或多位
  • \d+ - \)
  • ) - 字符串结尾(隐含在$

C++ demo

regex_match

输出:

regex check(R"(\([-+]?\d+,[-+]?\d+\))");
string s1("(44,45)");
string s2("(44,45");
smatch match;
if (regex_match(s1, match, check)) {
        cout << s1 << ": Matched!" << endl; 
} else {
    cout << s1 << ": Not matched!" << endl; 
}
if (regex_match(s2, match, check)) {
        cout << s2 << ": Matched!" << endl;     
} else {
    cout << s2 << ": Not matched!" << endl; 
}

答案 1 :(得分:0)

尝试输入此正则表达式\(\d{1,},\d{1,}\)

也许它可能有用