正则表达式字母、数字、破折号和下划线 [英] Regex Letters, Numbers, Dashes, and Underscores

查看:133
本文介绍了正则表达式字母、数字、破折号和下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何实现此匹配表达式.目前我正在使用,

Im not sure how I can achieve this match expression. Currently I am using,

([A-Za-z0-9-]+)

...匹配字母和数字.我还想在同一个表达式中匹配破折号和下划线.有人知道怎么做吗?

...which matches letters and numbers. I would also like to match on dashes and underscores in the same expression. Anyone know how?

我希望能够匹配 product_nameproduct-name

I would like to be able to match product_name and product-name

推荐答案

只需对破折号进行转义以防止它们被解释(我认为下划线不需要转义,但它不会造成伤害).你没有说你使用的是哪个正则表达式.

Just escape the dashes to prevent them from being interpreted (I don't think underscore needs escaping, but it can't hurt). You don't say which regex you are using.

([A-Za-z0-9\-\_]+)

这篇关于正则表达式字母、数字、破折号和下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆