我想检测一下,如果URL有语言代码。它可以是en
或sk
,但将来会有更多。
这些是我想尝试的网址:
1 - http://localhost/website/en
2 - http://localhost/website/endorphin
3 - http://localhost/website/en/
4 - http://localhost/website/en/test
5 - http://localhost/website/sk/entity
因此,例如,我想知道URL是否有en
语言代码。我尝试上面写的URL,结果是:
1 - false - should be TRUE
2 - false - OK
3 - true - OK
4 - true - OK
5 - false - OK
我试过的模式:/(\/en[\/{0,1}|\b])/mi
。
实时预览:PHP Sandbox
答案 0 :(得分:2)
以下正则表达式适用于en
或sk
\/(en|sk)(?:\/|$)
<强> Regex Demo 强>
正则表达式细分
\/ #Match a / literally
( #Capturing group
en #Match en literally
| #Alternation(OR)
sk #Match sk literally
)
(?: #Non-capturing group
\/ #Match a / literally
| Alternation(OR)
$ #End of string
)