dsa5233
2014-07-25 08:56
浏览 181
已采纳

如何在此正则表达式匹配中忽略引号中的字符串?

How can I ignore instances of the string "or" when it is inside single/double quotes?

The current expression is /^(?=\$)(.+?)(?:\s+or\s+)(.+?)$/s

Test value 1: $test or "default value"

Test value 2: $errors->has('email') ? 'error or failure' : ''

Test value 1 should be affected, but value 2 should be unaffected.

Test script:

Update $expression to test.

<?php

function issetDefaults($value) {

    // Original expression with the issue
    //$expression = '/^(?=\$)(.+?)(?:\s+or\s+)(.+?)$/s';

    // @Avinash Raj's version; almost there; failing on test 2
    $expression = '/([\'"])(?:(?!\1).)*or(?:(?!\1).)*\1(*SKIP)(*F)|^(\S+) or ([\'"].*[\'"])/';
    return preg_replace($expression, 'isset($2) ? $2 : $3', $value);
}

// Tests
$tests = array(

    // should be affected
    '$test or "default value"',
    '$age or 90',

    // shouldn't be affected
    'myfunc(\'foo or bar\')',
    '$errors->has(\'email\') ? \'error or failure\' : \'\'',
    '$errors->has("email") ? "error or failure" : ""',
    '$errors->has("email") ? "error \'or\' failure" : ""'
);


// Output tests
foreach ($tests as $key => $test) {
    $num = $key+1;
    echo '<strong>Original Value '.$num.'</strong>';
    echo '<pre>'.print_r($test,true).'</pre>';

    echo '<strong>Value '.$num.' after function</strong>';
    echo '<pre>'.print_r(issetDefaults($test),true).'</pre>';

    echo '<hr />';
}

图片转代码服务由CSDN问答提供 功能建议

当单词/双引号内的字符串“或”时,如何忽略该字符串的实例?

当前表达式是 / ^(?= \ $)(。+?)(?:\ s +或\ s +)(。+?)$ / s < / p>

测试值1: $ test或“default value”

测试值2: $ errors-&gt ;有('电子邮件')? '错误或失败':''

测试值1应该受到影响,但值2应该不受影响。

测试脚本:

更新 $ expression 进行测试。

 &lt;?php 
 
function issetDefaults($ value){
 
 //带问题的原始表达式
 // $ expression ='/^(?=。$)(。+  ?)(?:\ s +或\ s +)(。+?)$ / s'; 
 
 // @Avinash Raj的版本; 差不多了; 测试失败2 
 $ expression ='/([\'“])(?:(?!\ 1)。)*或(?:(?!\ 1)。)* \ 1(* SKIP)(  * F)| ^(\ S +)或([\'“]。* [\'”])/'; 
返回preg_replace($ expression,'isset($ 2)?$ 2:$ 3',$ value);  
} 
 
 //测试
 $ tests = array(
 
 //应受影响
'$ test或“默认值”',
'$ age或90',
 \  n //不应该受影响
'myfunc(\'foo或bar \')',
'$ errors-&gt;有(\'email \')?\'错误或失败\':\'  \'',
'$ errors-&gt;有(“电子邮件”)?“错误或失败”:“”',
'$ errors-&gt;有(“电子邮件”)?“错误”或\  '失败':“”'
); 
 
 
 //输出测试
foreach($ tests as $ key =&gt; $ test){
 $ num = $ key + 1; 
 echo'  &lt; strong&gt;原始值'。$ num。'&lt; / strong&gt;'; 
 echo'&lt; pre&gt;'。print_r($ test,true)。'&lt; / pre&gt;'; 
 
 echo  '&lt; strong&gt; Value'。$ num。' 在函数&lt; / strong&gt;'; 
 echo'&lt; pre&gt;'。print_r(issetDefaults($ test),true)。'&lt; / pre&gt;'; 
 
 echo'&lt; hr /&gt;'  ; 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpkpaxhzffixp8426 2014-07-25 09:05
    已采纳

    The below regex would matches the string or which was not enclosed within single or double quotes,

    (['"])(?:(?!\1).)*or(?:(?!\1).)*\1(*SKIP)(*F)|\bor\b
    

    Replace or with whatever string you want.

    DEMO

    Explanation:

    • (['"]) Captures ' or " symbols.
    • (?:(?!\1).)* Matches any character not the one which was captured into the first group zero or more times.
    • or Matches the string or.
    • (?:(?!\1).)* Matches any character not the one which was captured into the first group zero or more times.
    • \1 First captured group was referred through back referencing.
    • (*SKIP)(*F) Makes the whole match to fail and the characters which are following | symbol(\bor\b) would be matched from the remaining part.
    点赞 评论

相关推荐 更多相似问题