因此,我使用Shunting-Yard算法处理来自XML字符串的一些数学运算。诀窍是,我希望允许通过使用逗号分隔的列表来生成随机值。例如..。
( ( 3 + 4 ) * 12 ) * ( 2, 3, 4, 5 ) )我已经有一个基本的调车场处理器在工作了。但我希望在处理表达式之前对字符串进行预处理,以便从列表中随机选取一个值。这样我可能会得到这样的结果:
( ( 3 + 4 ) * 12 ) * 4 )就我的理解而言,调车场的设置已经相当复杂了,所以我在犹豫是否要改变它来处理这个问题。使用错误检查来处理它听起来像是一场噩梦。因此,我假设事先寻找该模式是有意义的?我在考虑使用正则表达式,但我不是“那些”人中的一员。尽管我希望我是...虽然我已经找到了一些examples,但我不确定如何修改它们以首先检查括号?我也不确定这是不是最好的解决方案。
顺便说一句,如果解决方案是regex,那么它也应该能够匹配逗号列表中的字符串(只匹配字符,不匹配符号),因为我将在我的Shunting-Yard实现中处理值的特定字符串。
提前感谢你的想法。
发布于 2011-04-25 23:51:54
使用两个正则表达式即可轻松解决此问题。第一个正则表达式应用于整个文本,与逗号分隔值的每个括号列表相匹配。第二个正则表达式应用于每个先前匹配的列表,匹配列表中的每个值。下面是一个PHP脚本,它有一个函数,给定一个具有多个列表的输入文本,将每个列表替换为随机选择的一个值:
<?php // test.php 20110425_0900
function substitute_random_value($text) {
$re = '/
# Match parenthesized list of comma separated words.
\( # Opening delimiter.
\s* # Optional whitespace.
\w+ # required first value.
(?: # Group for additional values.
\s* , \s* # Values separated by a comma, ws
\w+ # Next value.
)+ # One or more additional values.
\s* # Optional whitespace.
\) # Closing delimiter.
/x';
// Match each parenthesized list and replace with one of the values.
$text = preg_replace_callback($re, '_srv_callback', $text);
return $text;
}
function _srv_callback($matches_paren) {
// Grab all word options in parenthesized list into $matches.
$count = preg_match_all('/\w+/', $matches_paren[0], $matches);
// Randomly pick one of the matches and return it.
return $matches[0][rand(0, $count - 1)];
}
// Read input text
$data_in = file_get_contents('testdata.txt');
// Process text multiple times to verify random replacements.
$data_out = "Run 1:\n". substitute_random_value($data_in);
$data_out .= "Run 2:\n". substitute_random_value($data_in);
$data_out .= "Run 3:\n". substitute_random_value($data_in);
// Write output text
file_put_contents('testdata_out.txt', $data_out);
?>substitute_random_value()函数调用PHP preg_replace_callback()函数,该函数匹配每个列表并将其替换为列表中的一个值。它调用_srv_callback()函数,该函数随机挑选出一个值并将其作为替换值返回。
给定此输入测试数据(testdata.txt):
( ( 3 + 4 ) * 12 ) * ( 2, 3, 4, 5 ) )
( ( 3 + 4 ) * 12 ) * ( 12, 13) )
( ( 3 + 4 ) * 12 ) * ( 22, 23, 24) )
( ( 3 + 4 ) * 12 ) * ( 32, 33, 34, 35 ) )
下面是运行该脚本的一个示例的输出:
Run 1:
( ( 3 + 4 ) * 12 ) * 5 )
( ( 3 + 4 ) * 12 ) * 13 )
( ( 3 + 4 ) * 12 ) * 22 )
( ( 3 + 4 ) * 12 ) * 35 )
Run 2:
( ( 3 + 4 ) * 12 ) * 3 )
( ( 3 + 4 ) * 12 ) * 12 )
( ( 3 + 4 ) * 12 ) * 22 )
( ( 3 + 4 ) * 12 ) * 33 )
Run 3:
( ( 3 + 4 ) * 12 ) * 3 )
( ( 3 + 4 ) * 12 ) * 12 )
( ( 3 + 4 ) * 12 ) * 23 )
( ( 3 + 4 ) * 12 ) * 32 )
请注意,此解决方案使用\w+来匹配由"word“字符组成的值,即A-Za-z0-9_。如果这不能满足您的需求,可以很容易地更改它。
Javascript编辑:这里是substitute_random_value()函数的版本:
function substitute_random_value(text) {
// Replace each parenthesized list with one of the values.
return text.replace(/\(\s*\w+(?:\s*,\s*\w+)+\s*\)/g,
function (m0) {
// Capture all word values in parenthesized list into values.
var values = m0.match(/\w+/g);
// Randomly pick one of the matches and return it.
return values[Math.floor(Math.random() * values.length)];
});
}https://stackoverflow.com/questions/5776422
复制相似问题