dtq26360
2019-05-22 04:18
浏览 102
已采纳

如何在char列表中包含单引号和双引号? [重复]

This question already has an answer here:

I wrote the regex expression like this

^[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]+$

Which is checking the the string is symbol only

I use the checker to check that the regex is correct.

Then I put it into my php code

$regex = "^[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]+$";
$regex = '^[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]+$';

Both I tried and its not work because the quote is broken by the quote in the regex.

I don't want to concat them like

$regex="{partA}".'"'."{partB}";

I think this is too difficult to read and not easy to maintenance

How do I solve this problem?

</div>

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

此问题已经存在 这里有一个答案:

  • PHP正则表达式:在 3个答案中找不到结尾分隔符'^' r
  • php脚本中的双引号回显 6 answers

    我写了这样的正则表达式

      ^ [  ! -  $%^&安培; *()_ + |〜='{} \ [\]:“;'&LT;&GT;,\ /] + $ 
       
    吗?。  
     

    检查字符串是 sy 仅限mbol

    我使用检查程序检查正则表达式是否正确。

    然后我将它放入我的PHP代码中

      $ regex =“^ [ - !$%^&amp; *()_ + |〜=`{} \ [\]:”;'&lt;&gt; ?,  。\ /] + $“; 
     $ regex ='^ [ - !$%^&amp; *()_ + |〜=`{} \ [\]:”;'&lt;&gt;?,。\。  /] + $'; 
       
     
     

    我试过它都没有用,因为引用被正则表达式中的引号打破了。

    我不想像

      $ regex =“{partA}”。'“'。”{partB}“; 
       
     
     

    我认为这太难阅读而且不易维护

    如何解决这个问题? < / DIV>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongzhenyin2001 2019-05-22 04:21
    已采纳

    Both of your regexes produce syntax errors in PHP due to there being quotation marks inside of the regex itself.

    If you don't want to concatenate the regex into two different parts, your best approach is to escape the quotation marks with backslashes:

    $regex = "^[-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]+$";
    $regex = '^[-!$%^&*()_+|~=`{}\[\]:";\'<>?,.\/]+$';
    

    Note that the latter is more preferable, as double quotes will parse any variables stored within the string; if there were any text after the $, and a corresponding variable, the variable's content would be injected into the regex:

    $sample = 'text';
    $regex = "^[-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]+$sample";
    
    echo $regex;
    // ^[-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]+text
    

    In addition to this, it's also slightly faster to use single quotes.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongzhanlian6289 2019-05-22 04:36

    For your first expression, here we can use \x27 and \x22 instead of quotes, and we might want to remove the start and end chars, maybe similar to:

    [-!$%^&*()_+|~=`{}\[\]:\x22;\x27<>?,.\/]+
    

    enter image description here

    DEMO

    RegEx Circuit

    jex.im visualizes regular expressions:

    enter image description here

    Or if I understand correctly, another alternative would be to collect all chars in the first capturing group, then save our desired alphanumeric in the second one, maybe similar to:

    ([\s\S].*?)([a-z0-9]+)?
    

    enter image description here

    Test

    $re = '/([\s\S].*?)([a-z0-9]*)/mi';
    $str = '{partA}".\'"\'."{partB}{partC12}".\'"\'."{partD109}{partA}".\'"\'."{partB}{partC12}".\'"\'."{partD109}';
    $subst = '$2 ';
    
    $result = preg_replace($re, $subst, $str);
    
    echo $result;
    

    const regex = /([\s\S].*?)([a-z0-9]*)/gmi;
    const str = `{partA}".'"'."{partB}{partC12}".'"'."{partD109}{partA}".'"'."{partB}{partC12}".'"'."{partD109}`;
    const subst = `$2 `;
    
    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);
    
    console.log('Substitution result: ', result);

    DEMO

    </div>
    
    评论
    解决 无用
    打赏 举报
  • dongyi8416 2019-05-22 05:27

    Maybe your regular expression can be as simple as ^(?!.*[#\\@])[[:punct:]]+$?
    [:punct:] is one of POSIX Bracket Expressions.
    Demo and explanation at https://regex101.com/r/4XnUzW/1/.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题