Regex php标记的正则表达式匹配忽略xml标记

Regex php标记的正则表达式匹配忽略xml标记,regex,Regex,如果- 用户输入包含php标记 你需要正则表达式吗?为什么不干脆做: if file contains '<?php' throw an error if file contains '<?' but *doesn't* contain '<?xml' throw an error 如果文件包含“您需要正则表达式吗?为什么不干脆做: if file contains '<?php' throw an error if file contains

如果-

  • 用户输入包含php标记
    
    
    你需要正则表达式吗?为什么不干脆做:

    if file contains '<?php'
        throw an error
    if file contains '<?' but *doesn't* contain '<?xml'
        throw an error
    

    如果文件包含“您需要正则表达式吗?为什么不干脆做:

    if file contains '<?php'
        throw an error
    if file contains '<?' but *doesn't* contain '<?xml'
        throw an error
    

    如果文件包含“您要查找的正则表达式是:

    /<\?(?!xml)(?:php|.{0,3})/s
    

    /您要查找的正则表达式是:

    /<\?(?!xml)(?:php|.{0,3})/s
    

    /如果使用此正则表达式,请记住:

    <\?(?!x)
    

    我已经厌倦了寻找更好的解决方案,但它仍然比可以运行的代码要好

    如果使用此正则表达式,请记住:

    <\?(?!x)
    

    我已经厌倦了寻找更好的解决方案,但它仍然比可以运行的代码要好

    谢谢你的回答。我最初没有使用regex,当时我只需要检查是否存在PHP标记。后来,当我需要额外检查XML标记时,我选择了正则表达式。我想,我不太清楚什么时候该改成正则表达式匹配。在我的情况下,正则表达式会是一种过度杀伤力吗?无论如何,如果我使用这个逻辑
    如果(文件中包含“太好了!这是我一直在寻找的负面前瞻。顺便说一句,我们在生产中有PHP 5。在推出之前如何确保?对此有什么想法吗?谢谢你的回答。我最初没有使用正则表达式,当时我只需要检查PHP标记的存在。后来需要额外检查XML tags,我选择了正则表达式。我想,我对何时切换到正则表达式匹配的基本原理不是很清楚。在我的情况下,正则表达式会是一种过度的杀伤力吗?无论如何,如果我使用这个逻辑
    如果(文件中包含“太好了!这是我一直在寻找的负面前瞻。顺便说一句,我们的产品中有PHP5。但在推出之前如何确保?对此有什么想法吗?我认为在OP的情况下,解析输入比使用正则表达式更合适。我认为在OP的情况下,解析输入比使用正则表达式更合适。)。
    
    if file contains '<?php'
        throw an error
    if file contains '<?' but *doesn't* contain '<?xml'
        throw an error
    
    <\?(?!x)
    
    <\?([^x]|$)
    
    /<\?(?!xml)(?:php|.{0,3})/s
    
    if (preg_match("/<\?(?!xml)(?:php|.{0,3})/s", $str))
      echo "Not allowed\n";
    
    <\?(?!x)
    
    <\?([^x]|$)
    
    <?xml_error_string(1); echo 'Hello World!'; ?>
    
    <(\?([^x]|xml_.*?)|\%)
    
    error_string(1); echo 'Hello World'; ?>