【发布时间】:2012-12-19 20:12:12
【问题描述】:
可能重复:
How can I get a regex to check that a string only contains alpha characters [a-z] or [A-Z]?
PHP: How to check if a string starts with a specified string?
我尝试编写自己的正则表达式,但我糟透了。
#^(AJ\_)?.*#
问题是,我将创建一个这样的字符串:AJ______ClassNameBlahBlahBlah,我的函数返回 TRUE。我只需要AJ_ 和它后面的文字。
function isAnAJMClass($classname) {
if (preg_match('#^(AJ\_)?.*#', $classname)) {
return TRUE;
} else {
return FALSE;
}
}
【问题讨论】:
-
您指定了 .* 并且下划线是有效字符。您的规则是“以 AJ_ 开头,然后是任何内容”。你想要它是什么?
-
我想要
AJ_和AJ_之后的文字 -
对你来说什么是“文本”?就像我说的,
.将匹配任何字符,所以“______fooBar”是“文本”。如果您只想要字母,请使用 [a-z] -
我正在寻找 1 个下划线