开发者

Regex to extract block of data

Would somebody care to help me out with a preg_match_all Regex?

I need to extract from a block that looks like this:

(arbitrary data)
alt=BAUSTEIN^550^^transparent^transparent^null^null^(...base64 encoded data...) ^
(arbitrary data)
alt=BAUSTEIN^550^^transparent^transparent^null^null^(...base64 encoded data...) ^

all base64 encoded blocks. Th开发者_运维知识库e rule is: There is always alt=BAUSTEIN followed by six columns of arbitrary data delimited by ^. The base64 encoded column is also delimited by ^

my current feeble attempt contains a lot of ([^\^].*) and won't match anything. Pointers much appreciated.


Try this:

alt=BAUSTEIN(?:\^.*?){6}\^(?<base64>.*?)\^


I don't understand your example very well, but would this do it?

alt=BAUSTEIN\^+(.+?)\^+(.+?)\^+(.+?)\^+(.+?)\^+(.+?)\^+(.+?)\^+

Or a more refined one:

^alt=BAUSTEIN\^+(.+?)\^+(.+?)\^+(.+?)\^+(.+?)\^+(.+?)\^+([0-9a-zA-Z+/=]+)\^+$


here's one way without regex. since you have distinct delimiters, you can use splitting approach.

$str= <<<A
(arbitrary data)
alt=BAUSTEIN^550^^transparent^transparent^null^null^(...base64 encoded data...) ^
(arbitrary data)
alt=BAUSTEIN^550^^transparent^transparent^null^null^(...base64 encoded data...)
A;

$s = explode("^",$str);
for($i=0;$i<count($s);$i++){
    #check for alt=BAUSTEIN , if yes, go 6 indices forward to get your stuff
    if ( strpos($s[$i] ,"alt=BAUSTEIN" ) !==FALSE){
        print $s[$i+7]."\n";
    }
}


try this

$regex ="@^alt=@BAUSTEIN\^{2}[a-zA-Z]{1}\^[a-zA-Z]{1}\^(.*)"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜