正则表达式只允许使用字母数字,逗号,连字符,下划线和分号

正则表达式只允许使用字母数字,逗号,连字符,下划线和分号

问题描述:

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

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).

The following works:

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

But the following does not:

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

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, ',' );
}

Thanks for any help :)

我已经有了一些工作代码,但我需要有人帮助解释它为什么会起作用! p>

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

以下工作原理: p>

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

但以下内容不是:

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

为什么只有在逗号和分号开头时才能使用它? 把它们放在最后似乎打破了事情(这是我最初尝试时遇到的事情/ [^^a-zA-Z0-9_-]/s.

nn

除此之外, 我还使用以下内容修剪任何尾随 strong>分号(复数)或逗号(复数),有人可能会建议一种更有效和/或更优雅的方法来执行此操作?: p>

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

感谢您的帮助:) p> div>

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);

I believe it's the placement of the hyphen that matters -- has to be at start or end to be a hyphen (literal), otherwise it's being used to define a range.

You can escape the hyphen and put it anywhere in the regex like this \-

As for the trailing semicolons and commas, try this /[,;]+$/ it should match any commas and semicolons at the end even if they are many.