开发者

regex tweaking advice needed

I have this regex which some kind folks on SO helped me with yesterday. Anyway I have been editing it to do another match now and have almost got it. Basically in the following text I need it to match the space before the year and to append a pipe | to that match. I can get it to match but it includes the first digit of the year, what should I do to only match the space. By the way the year may not just be a four di开发者_运维技巧git sequence it may also be 2206 & 2007 or 2004-2008 and so on.

105| Ryan, T.N. 2005. |

$(?:[^|]+\|){1}(.*?)\.\s\d


If I understand your question correct, you want to add a | between Ryan, T.N.and 2005..

try this regex

^((?:[^|]+\|){1}.*?\.\s)(?=\d)

and replace with $1 and |

See it here on Regexr

The (?=\d) is positive lookahead, it ensures, that there is a digit ahead without matching it.


This should work if I understand you correctly.

$string = preg_replace("#(.*) ([0-9]+.)#", "$1 | $2", "105| Ryan, T.N. 2005. |");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜