正则表达式仅允许使用字母数字,逗号,连字符,下划线和分号 [英] Regex to only allow alphanumeric, comma, hyphen, underscore and semicolon

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

问题描述

我已经有一些工作代码,但是我需要一个人来帮助解释为什么他们可以工作!

I've already got a bit of working code but I need someone to help explain why it works if they can!

如果不是az,AZ,0-9,逗号,分号,下划线或连字符(它们最终应代表单个用户名或逗号),我正在使用PHP替换字符串中的任何内容/分号分隔的用户名列表.

I am using PHP to replace anything in a string if it is not either a-z, A-Z, 0-9, a comma, a semicolon, an underscore or a hyphen (which ultimately should represent either a single username, or a comma/semicolon separated list of usernames).

以下作品:

$data = preg_replace('/[^,;a-zA-Z0-9_-]/s', '', $data);

但是以下内容却没有:

$data = preg_replace('/[^a-zA-Z0-9_-,;]/s', '', $data);

为什么仅当逗号和分号开头时才起作用?将它们放在末尾似乎会破坏事情(这是我最初遇到/[^ a-zA-Z0-9 _-]/s/s时尝试的方法.

Why will this only work when the comma and semicolon are at the start? Putting them at the end seems to break things (this is what I tried initially when I came across /[^a-zA-Z0-9_-]/s.

顺便说一句,我还使用以下内容来修剪任何后缀分号(复数)或逗号(复数),并且有人可能会建议一种更有效和/或更优雅的方法这个吗?:

As an aside, I am also using the following to trim any trailing semicolons (plural) or commas (plural) and someone may be able to suggest a more efficient and/or elegant way to do this?:

if(preg_match('/;$/', $data))
{
    $data = rtrim($data, ';' );
}
if(preg_match('/,$/', $data))
{
    $data = rtrim($data, ',' );
}

感谢您的帮助:)

推荐答案

不是由逗号和分号引起的问题;这是连字符.查看角色类的各个部分,并考虑它们的含义:

It's not the comma and semicolon causing your problem; it's the hyphen. Look at the parts of your character class and consider what they mean:

0-9 # Anything from '0' to '9', meaning 0, 1, 2, ... 9
A-Z # Anything from 'A' to 'Z', meaning A, B, C, ... Z
_-, # Anything from '_' to ',', meaning...uh...hmmm.

_,尚无明确的进展,因此正则表达式引擎不确定如何处理.在字符类中,如果希望连字符按字面意义进行解释,则它必须位于类的开头或结尾(或以反斜杠转义).因此,这些方法中的任何一个都可以工作:

There's no clear progression from _ to ,, so the regex engine isn't sure what to make of this. In character classes, if you want a hyphen to be interpreted literally, it needs to be at the very beginning or end of the class (or escaped with a backslash). So any of these will work:

[^,;a-zA-Z0-9_-]
[^-,;a-zA-Z0-9_]
[^a-zA-Z0-9_\-,;]

关于修剪末端,您可以在一个正则表达式替换中完成所有这些操作:

As for trimming off the end, you can do all of this in one regex replace:

$data = preg_replace('/[^,;a-zA-Z0-9_-]|[,;]$/s', '', $data);

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

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