duanpo1498 2010-01-06 23:01
浏览 49
已采纳

独自一人,正则表达式的一部分。 如果在它之前添加了一个零件,在它之后添加了另一个零件,它将停止工

UPDATE: I'm making progress, but this is hard!

The test text will be valid[REGEX_EMAIL|REGEX_PASSWORD|REGEX_TEST].

(The real life text is required|valid[REGEX_EMAIL]|confirmed[emailconfirmation]|correct[not in|emailconfirmation|email confirmation].)

([^|]+) saves REGEX_EMAIL, REGEX_PASSWORD and REGEX_TEST in an array.

^[^[]+\[ matches valid[

\] matches ]

^[^[]+\[ + ([^|]+) + \] doesn't save REGEX_EMAIL, REGEX_PASSWORD and REGEX_TEST in an array.

How to solve?

  • 写回答

1条回答 默认 最新

  • dsf6281 2010-01-06 23:14
    关注

    Why is it important to try to everything with a single regular expression? It becomes much easier if you extract the two parts first and then split the strings on | using explode:

    $s = 'valid[REGEX_EMAIL|REGEX_PASSWORD|REGEX_TEST]';
    $matches = array();
    $s = preg_match('/^([^[]++)\[([^]]++)\]$/', $s, $matches);
    $left = explode('|', $matches[1]);
    $right = explode('|', $matches[2]);
    print_r($left);
    print_r($right);
    

    Output:

    Array
    (
        [0] => valid
    )
    Array
    (
        [0] => REGEX_EMAIL
        [1] => REGEX_PASSWORD
        [2] => REGEX_TEST
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败