如何使用正则表达式识别不以斜杠字符 ('/') 结尾的字符串? [英] How do I recognize strings that do not end with a slash character ('/') using a regex?
本文介绍了如何使用正则表达式识别不以斜杠字符 ('/') 结尾的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何匹配不以 /
结尾的字符串.我知道我可以这样做 /\/$/
并且如果字符串确实以 /
结尾,它将匹配,但是我如何测试以查看它是否没有?
How can i match a string that does not finish with /
. I know I can do that /\/$/
and it will match if string does finish with /
, but how can I test to see if it doesn't?
推荐答案
您可以使用否定的 字符班级:
/[^\/]$/
然而,这要求字符串至少包含一个字符.如果您还想允许空字符串,您可以使用替代:
This however requires that the string contains at least one character. If you also want to allow the empty string you can use an alternation:
/[^\/]$|^$/
另一种方法是使用否定的 lookbehind 但请注意许多流行的正则表达式引擎不支持后视:
A different approach is to use a negative lookbehind but note that many popular regular expression engines do not support lookbehinds:
/(?<!\/)$/
这篇关于如何使用正则表达式识别不以斜杠字符 ('/') 结尾的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文