PHP正则表达式跳过第一场比赛

时间:2013-11-10 10:11:54

标签: php regex

我有以下数据:

<h5> MY DATA </h5>
» Test data1
» Test data2
» Test data3

除了第一个,我希望匹配所有'»'。但是我尝试过的各种正则表达式都不起作用。请建议一些解决方案。

由于

2 个答案:

答案 0 :(得分:2)

为什么你想匹配除第一个之外的每个»?如果你告诉我们你想要完成什么,而不是你想要完成它,你会得到更好的回应。

根据我的理解,你有两行或更多行以某个字符开头,你想在除最后一行之外的每一行的末尾添加一个<br/>标记。当你用这种方式描述它时,正则表达式实际上是自己写的:

^        # beginning of line (in multiline mode)
(».+\R)  # `»` and the rest of the line, including the trailing newline (`\R`)
(?=»)    # lookahead checks that the next line begins with `»`, too

该行已在第1组中捕获,因此我们将其重新插入替换字符串并添加<br/>标记:

$result = preg_replace('/^(».+\R)(?=»)/m', '$1<br/>', $subject);

我不熟悉PHP,但你可能需要添加UTF8修饰符(/^(».+\R)(?=»)/mu)或使用»字符的{hex}转义符/^(\x{BB}.+\R)(?=\x{BB})/m

答案 1 :(得分:1)

你可以试试这个:

$result = preg_replace('~[^>\s]\h*\R\K(?=»)~', '<br/>', $string);

细节:

[^>\s]  # a character that is not a white char or a > (to avoid the first line)
\h*     # horizontal white chars zero or more times (possible leading spaces)
\R      # a new line
\K      # remove all that have been matched before
(?=»)   # lookahead assertion, to check if there is a » after

模式的目标是匹配字符串中良好位置的空字符串。