2013-02-11 99 views
1

只是好奇知道。我注意到,與「\ B」正則表達式邊界和字符串'c#'

if (preg_match('@\bC#\[email protected]', $userSkills)) {... 

但是使用它,如果我刪除「\ B」它設法找到字符串時,我的正則表達式無法找到字符串「C#」。

有人知道這裏可能是什麼問題嗎?

字符串以查找匹配:

Unified Communications Design Specialist ~$~ Windows 95 or 98 ~$~ Windows 2000 
or XP ~$~ Linux ~$~ Visual Basic ~$~ ASP Net ~$~ C# ~$~ Contact Center Design 
Specialist ~$~ Internet Programming ~$~ Object oriented programming ~$~ 
Databases ~$~ Unified Commucations ~$~ Contact Center Solutions ~$~ 
Junior Developer ~$~ Solutions Engineer 
+0

你在正則表達式中使用了哪一個?'\ b'或'/ b'? – 2013-02-11 14:25:48

+0

我發佈了我試圖找到匹配的字符串。它適用於除c#之外的所有字符串,據我注意到 – vodich 2013-02-11 14:47:52

回答

1

這是因爲#是無字的字符。我在「C#」周圍的字符串中標記了字邊界,內容爲|

|ASP| |Net| ~$~ |C|# ~$~ |Contact| 

你看, 「#」 和 「」 之間沒有邊界,所以\b不匹配。

+0

爲什麼downvote?問題是,*爲什麼*'\ b'不匹配,我解釋了它。 – 2013-02-11 17:49:54

+0

因此,除了str_replace('#','sharp',$ var)之外,這種情況下的任何解決方法? – vodich 2013-02-11 18:17:10

+0

你可以使用'[\ s ^](C#)[\ s $]',以便在「C#」之前或之後匹配空格或「開始」/「結束」。當然,整個模式然後匹配「C#」*,包括那些空格,因此是子模式括號。 – 2013-02-11 18:35:53

2

escape sequence\b/b word邊界。

if (preg_match('@\bC#\[email protected]', $userSkills)) {... 
+0

對不起在我的問題中犯了一個錯誤,我現在編輯它,但問題仍然存在 – vodich 2013-02-11 14:24:37

+0

在OP徹底改變問題之前,100%正確的答案downvote?很可笑... – nickb 2013-02-11 14:32:32

+0

好吧,你的答案是不正確的,無論斜槓/反斜槓......似乎有不同類型的問題在這裏 – vodich 2013-02-11 14:50:49