这是我的版本。
如果第一个不匹配,它使用后备正则表达式(如前所述)。
演示
代码:
<?php
function do_replace($string) {
$regex = '/^(\((?:<([a-z])>)?(\d{0,3}|[a-z]{1,3})(?:<\/\2>)?(\.)?\)|\[(?:<([a-z])>)?(\d{0,3}|[a-z]{1,3})(?:<\/\2>)?(\.)?\])\s*(.*)/i';
$result = preg_match($regex, $string);
if($result) {
return preg_replace($regex, '%%$1|$8', $string);
} else {
$regex = '/^(\d{0,3}|[a-z]{1,3})\.\s*(.+)$/i';
$result = preg_match($regex, $string);
if($result) {
return preg_replace($regex, '%%$1.|$2', $string);
} else {
return $string;
}
}
}
$strings = array(
'(1)blahblah',
'(<i>iv</i>.) blahblah',
'[b] some stuff',
'25. blahblah',
'A. some other stuff. one',
'blah. some other stuff',
'text (1) text',
'2008. blah',
'[123) <-- mismatch'
);
foreach($strings as $string) echo do_replace($string) . PHP_EOL;
?>
第一个正则表达式展开:
$regex = '
/
^(
\(
(?:<([a-z])>)?
(
\d{0,3}
|
[a-z]{1,3}
)
(?:<\/\2>)?
(\.)?
\)
|
\[
(?:<([a-z])>)?
(
\d{0,3}
|
[a-z]{1,3}
)
(?:<\/\2>)?
(\.)?
\]
)
\s*
(.*)
/ix';