这很容易解决使用两个正则表达式。适用于整体文本的第一个正则表达式匹配逗号分隔值的每个加括号列表。应用于每个先前匹配列表的第二个正则表达式匹配列表中的每个值。下面是有功能的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) * 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+
以匹配由 “字” 的字符,即,[A-ZA-Z0-9_]值。如果这不符合您的要求,可以轻松更改。
编辑:这里是substitute_random_value()
功能的JavaScript版本:
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)];
});
}
您正在使用什么语言,解析这个? – ridgerunner 2011-04-25 14:53:40
AS3,所以虽然我很欣赏具体的答案,但我试着让问题更一般。 – grey 2011-04-26 03:17:55
当提出正则表达式问题时,指定语言很重要,因为每种语言都有不同的正则表达式能力(正确答案取决于这些能力)。例如,现在我知道您正在使用AS3(使用Javascript正则表达式“flavor”),实际上正确的答案是简化的。看到我更新的答案。 – ridgerunner 2011-04-26 13:53:37