2011-04-08 258 views
1

我有類似這樣的字符串:正則表達式表達

Hi, <<\name>> <<\surname>>, this is an example <<\test>>. 

我什麼匹配的正則表達式和拆分此字符串:

"Hi, " 
<<\name>> 
" " 
<<\surname>> 
", this is an example " 
<<\test>> 
"." 

我想這一個:(<<\*.*?>>)|(>>*.*?<<),但沒有按」工作。這是可能的? 謝謝。

+0

是<<*.?>>一個錯字?這將是錯誤的。它是<<.*?>> – duedl0r 2011-04-08 13:42:08

+0

無論如何你想用(>>。*?<<)做什麼?它對你的解決方案沒有幫助,是嗎?並可能甚至迷惑正則表達式 – duedl0r 2011-04-08 13:57:24

回答

0

使用str_replace

$str =' Hi, <<\name>> <<\surname>>, this is an example <<\test>>.'; 
$old= array('<<','>>'); 
$new =array('"<<','>>"'); 
$str2=str_replace ($old,$new,$str); 
//retruns Hi, "<<\name>>" "<<\surname>>", this is an example "<<\test>>". 
+1

也許他不是在PHP中開發? – duedl0r 2011-04-08 13:55:33

+0

對不起,但我想拆分不能取代。 Im'在VB.NET中開發 – batdan 2011-04-08 14:14:02

2

你可以使用這個表達式(它採用積極的向前看符號和積極lookbehinds):

<<.*?>>|(?<=>>|^).*?(?=<<|$) 

它匹配由<<>>符號括起來的字符串,字符串這是它們之間符號和字符串從開頭到<<,最後從>>結束。

您可以在ideone上找到C#示例。

+0

非常感謝Alex ......就是這樣! 唯一的一點是,這個正則表達式沒有太多的回車,但從來不知道。 – batdan 2011-04-08 15:13:13