简体   繁体   English

PHP:正则表达式匹配以下字符串样本

[英]PHP: Regex to match the following string samples

I've been constantly attempting to adapt a perfect string matching expression for my syntax highlighter, but it's time to ask for help.我一直在尝试为我的语法荧光笔调整一个完美的字符串匹配表达式,但现在是寻求帮助的时候了。

<?php

    if ( !empty( $_GET['gamertag'] ) )
    {

        require_once( 'xbox.php' );
        header( 'Content-Type: image/png' );

        // Content
        $xbox = new Xbox( $_GET['gamertag'] );
        $font = 'fonts/helr65w.ttf';

        // Images
        $bg = @imagecreatefrompng( 'content/gamercard.png' );
        $gp = @imagecreatefrompng( $xbox->Links['GamerPicture'] );
        $st = @imagecreatefrompng( 'content/star.png' );
        $re = Array();

        if ( $xbox->CanViewGames() )
        {

            foreach( $xbox->RecentGames as $key => $value )
                $re[] = @imagecreatefromjpeg( $value['Image'] );

        }

        // Save Transparency
        @imagesavealpha( $bg, true ); 
        @imagealphablending( $bg, false ); 
        @imagecolorallocatealpha( $bg, 255, 255, 255, 127 );
        @imagealphablending( $bg, true );

        // Create Colors
        $white = @imagecolorallocate( $bg, 255, 255, 255 );
        $grey = @imagecolorallocate( $bg, 128, 128, 128 );
        $black = @imagecolorallocate( $bg, 0, 0, 0 );
        $orange = @imagecolorallocate( $bg, 233, 171, 23 );

        // Write Information

        for( $i = 0; $i < count( $re ); $i++ ) // Recent Games
            @imagecopy( $bg, $re[$i], ( 100 + ( 40 * $i ) ), 44, 0, 0, 32, 32 );

        for( $r = 0; $r < $xbox->Reputation; $r++ ) // Reputation
            @imagecopy( $bg, $st, ( 196 + ( $r * 20 ) ), 125, 0, 0, 16, 16 );

        @imagecopy( $bg, $gp, 18, 55, 0, 0, 64, 64 );
        @imagettftext( $bg, 14, 0, 40, 30, $black, $font, $xbox->Gamertag );
        @imagettftext( $bg, 14, 0, 143, 105, $black, $font, $xbox->Gamerscore );
        @imagettftext( $bg, 6, 0, 7, 161, $black, $font, $xbox->CurrentActivity );
        @imagepng( $bg );

        // Destroy Images
        @imagedestroy( $bg );
        @imagedestroy( $gp );
        @imagedestroy( $st );

        foreach( $re as $game )
            @imagedestroy( $game );

    }
    else
    {

        print( '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

    <head>

        <title>Xbox Gamercard</title>

        <script type="text/javascript">

            function go()
            {

                var gamertag = document.getElementById( "gamertag" ).value;
                window.location.href = ( gamertag + ".png" );

            }

        </script>

    </head>

    <body>

        <input type="text" id="gamertag" onkeydown="if (event.keyCode == 13) go();" />
        <input type="button" value="Submit" onclick="go();" /><br /><br />

        <a href="/projects/xbox-gamercard/ttg/" title="TTG Signature Gamercard">TTG Signature Gamercard</a>

    </body>

</html>' );

    }

?>

I'm looking for an expression that can successfully match all content between '' and "" .我正在寻找一个可以成功匹配''""之间所有内容的表达式。

It needs to ignore all (correctly) escaped versions of themselves.它需要忽略自己的所有(正确)转义版本。 (So content between '' will ignore \' , and content between "" will ignore \" ) (因此''之间的内容将忽略\' ,而""之间的内容将忽略\"

It doesn't matter which one comes first.哪个先来并不重要。

Here are two expressions I have already tried:这是我已经尝试过的两种表达方式:

"/'[^'\\\\]*(?:\\\\.[^'\\\\]*)*?'/"
"/'(.[^']*)'/"

Try this raw regex.试试这个原始的正则表达式。 Group 2 would contain your strings.第 2 组将包含您的字符串。

(['"])((?:\\\1|(?!\1).)+)\1

Since PHP isn't my language I'm not sure I escaped it correctly, but try this:由于 PHP 不是我的语言,我不确定我是否正确地转义了它,但试试这个:

$pattern = '/([\'"])((?:\\\\\\1|(?!\\1).)+)\\1/';

This one won't catch commented code appropriately, for example:这不会正确捕获注释代码,例如:

// ' my string does not get terminated.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM