简体   繁体   中英

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 digit 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, TN 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.

如果我对您的理解正确,那应该可以。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM