2017-04-06 52 views
1

我已經在MySQL數據庫(5.7.17-0ubuntu0.16.04.2)中加載了路徑,並且用戶選擇了路徑選擇,我想選擇所有這些路徑和下面的,但我遇到了一個問題。MySQL REGEXP Match/or End of Field

假定用戶要「/根/ K」我需要爲做一個選擇:

a. /root/K% 
b. /root/K 

我怎樣才能獲得REGEXP到現場或/結束匹配嗎?

我已經試過如下:

原始查詢:

where path REGEXP ('/root/K/|/root/J/J/') # this works but doesn't show the items in that path, only ones below 

where path REGEXP '/root/K[/\z]' # does the same as above 
where path REGEXP '/root/K(?=/|$)' # Get error 1139, repetition-operator invalid 

我也試過:Regex to match _ or end of string但給人錯誤1139

任何其他建議?

+1

完成,謝謝你的幫助。 – trevrobwhite

回答

1

有沒有支持lookarounds,而不是\z錨定在MySQL的正則表達式。您可以使用正常的捕獲組:

WHERE path REGEXP '/root/K(/|$)' 

它將匹配

  • /root/K - 字面字符序列
  • (/|$) - 無論是/或進入結束。