duancong2965 2013-09-29 12:28
浏览 60
已采纳

使用preg_split拆分复杂的字符串

Update v2

Using the code by Jerry works on most strings, but not all of them, like:

$pattern = '#^(?<tz_utf>(?:\([^)]+\)|[^-]+)+)\s+-\s+(?<tz>[^:]+)\s+:\s+(?<fr>[^/]+)\s+/\s+(?<en>[^/]+)\s+/\s+(?<ar>\S+)\s+(?<tz_dec_utf>[ⴰ-⵿ -]+)\s+(?<tz_dec>.*)$#imu';

// In this string, it doesn't validate because of no space between slash & word;
// /Alphabet => / Alphabet
// and comma in Arabic;
// ájóéHCG ,á«é¡J => ájóéHCGá«é¡J
$str4 = 'ⴰⴳⵎⵎⴰⵢ - agemmay : Alphabet, épellation /Alphabet, spelling / ájóéHCG ,á«é¡J
    ⴰⴳⵎⵎⴰⵢ - ⵓⴳⵎⵎⴰⵢ - ⵉⴳⵎⵎⴰⵢⵏ
    agemmay  – ugemmay  – igemmayen';

$str5 = 'ⴰⴷⴷⴰⴷ ⴰⵎⴰⵔⵓⵣ - addad amaruz : Etat d’annexion / Construct state / ¥ÉëdEG ádÉM
    ⴰⴷⴷⴰⴷ ⴰⵎⴰⵔⵓⵣ - ⵡⴰⴷⴷⴰⴷ ⴰⵎⴰⵔⵓⵣ
    addad  amaruz  - waddad amaruz';

$str6 = 'ⴰⴷⴷⴰⴷ ⵉⵍⴻⵍⵍⵉ - addad ilelli : Etat libre / Free state / ∫É°SQEG ádÉM
    ⴰⴷⴷⴰⴷ ⵉⵍⴻⵍⵍⵉ
    addad  ilelli';


print_r( preg_match($pattern, $str, $matches) );

Update v1

The code I'm using now matches only one portion of the whole string ($matches[1]), is it possible to extract other portions of the string using one regex?:

$pattern = '/-(.*?)\:/';    
$str1 = 'ⵜⴰⵙⵎⵙⵙⵉⵜ - tasmessit : Focalisée / Focus / QCÉÑe ⵜⴰⵙⵎⵙⵙⵉⵜ - ⵜⵙⵎⵙⵙⵉⵜ - ⵜⵉⵙⵎⵙⵙⵉⵜⵉⵏ tasmssit - tsmssit - tismssitin';
preg_match($pattern, $str1, $matches);
$arr1 = array( 
        'tz_utf'=>'ⵜⴰⵙⵎⵙⵙⵉⵜ', 
        'tz'=> $matches[1], // tasmessit
        'fr'=>'Focalisée', 
        'en'=>'Focus', 
        'ar'=>'QCÉÑe', 
        'tz_dec_utf'=>'ⵜⴰⵙⵎⵙⵙⵉⵜ - ⵜⵙⵎⵙⵙⵉⵜ - ⵜⵉⵙⵎⵙⵙⵉⵜⵉⵏ', 
        'tz_dec'=>'tasmssit - tsmssit - tismssitin'
);
print_r($matches[1]); 

Original question

For any regular expression gurus out there :)

Can you please help preg_split some strings to an array? The string value may vary and look similar to this scheme:

$str1 = 'ⵜⴰⵙⵎⵙⵙⵉⵜ - tasmessit : Focalisée / Focus / QCÉÑe ⵜⴰⵙⵎⵙⵙⵉⵜ - ⵜⵙⵎⵙⵙⵉⵜ - ⵜⵉⵙⵎⵙⵙⵉⵜⵉⵏ tasmssit - tsmssit - tismssitin';
$str2 = 'ⵜⴰⵙⵏⴰⵥⵖⵓⵕⵜ ( ⵏ-) - tasnaÇvurt (n-)  : Etymologique / Etymological / »dÉKCG ⵏ ⵜⵙⵏⴰⵥⵖⵓⵕⵜ n tesnaÇvurt';
$str3 = 'ⵜⴰⵙⵖⵓⵏⵜ ⵜⴰⵏⴰⴷⴰⵡⵜ - tasvunt tanadawt : Subordonnant / Subordinating (conjunction) / §HGQ ⵜⴰⵙⵖⵓⵏⵜ ⵜⴰⵏⴰⴷⴰⵡⵜ - ⵜⵉⵙⵖⵡⴰⵏ ⵜⵉⵏⴰⴷⴰⵡⵉⵏ tasvunt tanadawt - tisevwan tinadawin';

The correct results would be;

$arr1 = array( 
                'tz_utf'=>'ⵜⴰⵙⵎⵙⵙⵉⵜ', 
                'tz'=>'tasmessit', 
                'fr'=>'Focalisée', 
                'en'=>'Focus', 
                'ar'=>'QCÉÑe', 
                'tz_dec_utf'=>'ⵜⴰⵙⵎⵙⵙⵉⵜ - ⵜⵙⵎⵙⵙⵉⵜ - ⵜⵉⵙⵎⵙⵙⵉⵜⵉⵏ', 
                'tz_dec'=>'tasmssit - tsmssit - tismssitin'
        );
$arr2 = array( 
                'tz_utf'=>'ⵜⴰⵙⵏⴰⵥⵖⵓⵕⵜ ( ⵏ-)', 
                'tz'=>'tasnaÇvurt (n-)', 
                'fr'=>'Etymologique', 
                'en'=>'Etymological', 
                'ar'=>'»dÉKCG', 
                'tz_dec_utf'=>'ⵏ ⵜⵙⵏⴰⵥⵖⵓⵕⵜ', 
                'tz_dec'=>'n tesnaÇvur'
        );
$arr3 = array( 
                'tz_utf'=>'ⵜⴰⵙⵖⵓⵏⵜ ⵜⴰⵏⴰⴷⴰⵡⵜ', 
                'tz'=>'tasvunt tanadawt', 
                'fr'=>'Subordonnant', 
                'en'=>'Subordinating (conjunction)', 
                'ar'=>'§HGQ', 
                'tz_dec_utf'=>'ⵜⴰⵙⵖⵓⵏⵜ ⵜⴰⵏⴰⴷⴰⵡⵜ - ⵜⵉⵙⵖⵡⴰⵏ ⵜⵉⵏⴰⴷⴰⵡⵉⵏ', 
                'tz_dec'=>'tasvunt tanadawt - tisevwan tinadawin'
        );

The tz_utf are Tifinagh charcters in unicode.

Thanks

  • 写回答

1条回答 默认 最新

  • dongzi5062 2013-09-29 14:03
    关注

    Try using the regex:

    ~^(?<tz_utf>(?:\([^)]+\)|[^-]+)+)\s+-\s+(?<tz>[^:]+)\s+:\s+(?<fr>[^/]+)\s+/\s+(?<en>[^/]+)\s+/\s+(?<ar>\S+)\s+(?<tz_dec_utf>[ⴰ-⵿ -]+)\s+(?<tz_dec>.*)$~ui
    

    Warning, I'm not sure about the special character part as from the Armenian characters (I used a \S+ for them assuming they are single word and I used a range from this site for the characters which appear like white squares), but it's working for the sample your provided.

    regex101 demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧