一个正则表达式,用于匹配带有字母数字,空格和标点符号的字符串 [英] A regex to match strings with alphanumeric, spaces and punctuation

查看:95
本文介绍了一个正则表达式,用于匹配带有字母数字,空格和标点符号的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个正则表达式来匹配包含字母,数字,空格和一些简单标点符号的字符串(.,!'/$ ).我有 ^ [A-Za-z0-9 _] * [A-Za-z0-9 _ [A-Za-z0-9 _] * $ ,它对字母数字和空格有效,但不适用于标点符号.非常感谢您的帮助.

I need a regular expression to match strings that have letters, numbers, spaces and some simple punctuation (.,!"'/$). I have ^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$ and it works well for alphanumeric and spaces but not punctuation. Help is much appreciated.

推荐答案

只需在类内(在方括号内)添加标点符号和其他字符:

Just add punctuation and other characters inside classes (inside the square brackets):

[A-Za-z0-9 _.,!"'/$]*

这匹配包含空格,_,字母数字,逗号,!,,$,...的每个字符串"添加一些特殊字符时请注意,也许您需要转义它们:更多信息在这里

This matches every string containing spaces, _, alphanumerics, commas, !, ", $, ... Pay attention while adding some special characters, maybe you need to escape them: more info here

这篇关于一个正则表达式,用于匹配带有字母数字,空格和标点符号的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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