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


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

How do I solve this problem?


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

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

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


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

    检查字符串是 sy 仅限mbol



      $ 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:


    enter image description here


    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:


    enter image description here


    $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);


    解决 无用
    打赏 举报
  • 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/.

    解决 无用
    打赏 举报

相关推荐 更多相似问题