2017-04-06 59 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 - 字面字符序列
  • (/|$) - 无论是/或进入结束。