Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式-匹配除正斜杠以外的所有内容_Regex - Fatal编程技术网

Regex 正则表达式-匹配除正斜杠以外的所有内容

Regex 正则表达式-匹配除正斜杠以外的所有内容,regex,Regex,在以下字符串中: /西雅图/餐厅 我想匹配西雅图(如果有)(有时url可能是/西雅图/餐厅,有时可能是/餐厅)。我不想匹配下面的正斜杠:西雅图/ 我尝试了以下方法,但无法使其工作: /(.*[^/]?)restaurant(\?.*)?$ 我需要第一个正斜杠,所以解决方案不是删除它,我可以这样做: (/?)(.*)/restaurant(\?.*)?$ 谢谢 托马斯像这样的东西怎么样 ^/([^/]+)/?(.*)$ 我用python对其进行了测试,似乎效果不

在以下字符串中:

/西雅图/餐厅

我想匹配西雅图(如果有)(有时url可能是/西雅图/餐厅,有时可能是/餐厅)。我不想匹配下面的正斜杠:西雅图/

我尝试了以下方法,但无法使其工作:

       /(.*[^/]?)restaurant(\?.*)?$
我需要第一个正斜杠,所以解决方案不是删除它,我可以这样做:

     (/?)(.*)/restaurant(\?.*)?$   
谢谢


托马斯

像这样的东西怎么样

^/([^/]+)/?(.*)$
我用python对其进行了测试,似乎效果不错:

>>> regex=re.compile(r'^/([^/]+)/?(.*)$')
>>> regex.match('/seattle').groups()
('seattle', '')
>>> regex.match('/seattle/restaurant').groups()
('seattle', 'restaurant')

我想,
'/settle/restaurant'.split('/')
也会这么做。