【问题标题】:Just remove a a-zA-z section in regular expression只需删除正则表达式中的 a-zA-z 部分
【发布时间】:2013-08-19 19:16:42
【问题描述】:

这是我的正则表达式字符串:

'(?!('.$exceptions.')((\W+)|$))[a-zA-Z\-_]+/?$'

$exceptions 是一个包含如下字符串的变量:

word1|word2|word3|word4|wordN

我只想删除 a-zA-Z 部分,这意味着我只想删除检查英文字符的规则,因为 unicode。

一个样本:

$exception ='word1|word3|word3|word4' ;
$myword = 'a-unicode-statement-like-سلام' ;

如果 $myword 与正则表达式规则字符串进行比较,它将不匹配,因为 سلام 它不在 a-zA-z 范围内,我只想删除此限制 (a-zA-Z)

【问题讨论】:

  • 我不明白你想要达到什么目的。尝试提供一些输入和输出示例。
  • $string='kjhkjdgdfلبیلبیل%%%^&*('我想要如果 $string 不等于 $excpetion 单词之一然后打印 true
  • edit你的问题。
  • 我只是不想检查 a-zA-z 因为 unicode 字符而且我不知道正则表达式规则。当删除 [a-zA-z] 然后得到错误
  • '(?!('.$exceptions.')((\W+)|$)).+/?$' ??

标签: php regex


【解决方案1】:

尝试添加一些内容以匹配其他所有内容,而不是您的 a-zA-Z 规则。

'(?!('.$exceptions.')(.*))'

编辑:

阅读下面的评论后。也许更好的解决方案是使用针对此问题提出的解决方案:wordpress: how to check if the slug contains a specific word?

然后您可以使用以下内容进行检查:

$url = $_SERVER["REQUEST_URI"];

$isException = strpos($url, 'word1');

if ($isException !== false)
{
    //url contains word in exceptions!
}

【讨论】:

  • 包含一个带有“-”或“_”字符的单词。问题是我不知道正则表达式
  • 我需要它来检查 cakephp 路由器中的 url 中的 slug
  • 非常感谢 Jonas 的关注,我更改了正则表达式并将 Mark 作为评论发送,效果很好 Router::connect('/:language/:typeslug', array('controller' => ' nodetypes', 'action' => 'view'), array('language'=>'[az]{3}', 'typeslug' => '(?!('.$exceptions.')((\W+ )|$)).+/?$', 'pass'=>array('typeslug') ) );
【解决方案2】:

据我了解,我认为您正在寻找这个:

$exceptions = ["word1","word2","word3"];
       // or $exceptions = explode("|",$exceptions) to work with what you have already
if( in_array($string,$exceptions)) {
   // word is in exceptions
}

【讨论】:

  • 只是想用正则表达式来做,只删除 a-zA-z 规则,只是不想因为 unicode 字符而检查字符是否在 a-z 或 A-Z 范围内。
猜你喜欢
  • 1970-01-01
  • 2010-12-11
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多