开发者

How can I port PHP preg_split to Java for the special case of unserializing a value in ADODB?

开发者 https://www.devze.com 2023-02-01 07:05 出处:网络
I need to port this function for unserializing a value in ADODB to Java. $variables = array( ); $a = preg_split( \"/(\\w+开发者_JAVA技巧)\\|/\", $serialized_string, -1, PREG_SPLIT_NO_EMPTY |PREG_SPLI

I need to port this function for unserializing a value in ADODB to Java.

        $variables = array( );
        $a = preg_split( "/(\w+开发者_JAVA技巧)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY |   PREG_SPLIT_DELIM_CAPTURE );
        for( $i = 0; $i < count( $a ); $i = $i+2 ) {
            $variables[$a[$i]] = unserialize( $a[$i+1] );
        }

I have a library to unserialize the values the php way, but I need help on porting over the preg_split. What would this regex look like in Java?


Equivalent java code :


import java.util.List;
import java.util.ArrayList;

// Test
String serialized_string = "foo|bar|coco123||cool|||";

// Split the test
String[] raw_results=serialized_string.split("\\|");// Trailing empty strings are removed but not internal ones

// Cleansing of the results
List<String> php_like_results = new ArrayList<String>();
for(String tmp : raw_results) {
    if (tmp.length()>0) {
       php_like_results.add(tmp);
    }
}

// Output results
System.out.println(php_like_results);

This will produce : [foo, bar, coco123, cool]

0

精彩评论

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