其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

简单的正则表达式匹配一些字符并排除其他字符

GG网络技术分享 2025-03-18 16:15 2


问题描述:

I need a regex for preg_match to accept all alphanumeric characters except l, L, v, V, 0, 2.

I\'ve tried

^[a-zA-Z0-9][^lLvV02]*$

It works good excluding lLvV02 but it also accept other characters like SPACE,ù,@,#, etc...

How should I change it?

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我需要preg_match的正则表达式接受除 l </ code>,之外的所有字母数字字符 L </ code>, v </ code>, V </ code>, 0 </ code>, 2 </ code>。</ p>

我试过</ p>

  ^ [a-zA-Z0-9] [^ lLvV02] * $ 

</ code> </ pre>

\\ n

排除lLvV02后效果很好,但它也接受SPACE,ù,@,#等其他字符...... </ p>

我该如何更改?</ p> \\ n </ div>

网友观点:

You may use

^(?:(?![lLvV02])[a-zA-Z0-9])*$

Details

  • ^ - start of string
  • (?: - start of a non-capturing group

    • (?![lLvV02])[a-zA-Z0-9] - an alnum char that is not one of the chars inside the character class residing inside a negative lookahead
  • )* - end of the non-capturing group, 0 or more repetitions
  • $ - end of string

See the Regulex graph:

I know you asked for a Regex, but you can test for alphanumeric first and only if that passes check that the others are NOT present:

if(ctype_alnum($string) && !preg_match(\'/[lLvV02]/\', $string)) {

//pass

} else {

//fail

}

Or possibly substitute preg_match(\'/^[^lLvV02]+$/\', $string).

Easiest would probably be: ^[a-km-uw-zA-KM-UW-Z13-9]*$.

I\'m not saying that it\'s pretty but it does what it\'s supposed to.

正则表达式匹配

描述

给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配。

'.' 匹配任意单个字符

'*' 匹配零个或多个前面的那一个元素

所谓匹配,是要涵盖整个字符串 s的,而不是部分字符串。

标签:

提交需求或反馈

Demand feedback