duanpin2034 2019-06-21 15:28
浏览 68
已采纳

复杂的正则表达式,用于获取字符串中花括号的数字

I need to get the number in curly braces in a complex string. Basically I am parsing some data, and I need to extract the ids that are represented in a curly braces.

Example string:

{1|{1078} {*|{1079}-}test{1|{4829}, test2 {4457}}} {*|{1078} {*|{1079}-}test3{1|{4829}, test4 {23232}}}

What I exactly need is to extract the number in curly braces that is near the pipe (|{4829}, |{1079}, |{1078}), and not any others numbers, so my end result would be something like:

4829,1079,1078

or array of this numbers, it does not matter. It should be unique values but that is not problem for me. My problem is create regex that will just extract those numbers. I have tried a lot of stuff during this day, latest one what I have tried is this:

public static function getAllAttributeIDsFromTheRule($attributeValues)
{

    preg_match_all('/{(.*?)}/', $attributeValues, $matches);
    preg_match_all('\|{\d*}', $attributeValues, $matches[1]);

    $attributeIDsWithPipe = (implode('', self::clean($matches[1])));

    $attributeIDs = explode('|', $attributeIDsWithPipe);

    var_dump($attributeIDs);

}

public static function clean($string) 
{
    $string = str_replace(' ', '-', $string);

    return preg_replace('/[^A-Za-z0-9|\-]/', '', $string);
}   

But I am always stuck with one other character in the result.In some result I get number extra or something like that. Now, it is time to ask for help if someone knows the better approach. Much appreciated.

  • 写回答

2条回答 默认 最新

  • duanping6698 2019-06-21 15:30
    关注

    You may use a regex to match |{, some 1+ digits, and } and capture the digits inside into a capturing group, and then just access the values from the group using $matches[1]:

    if (preg_match_all('~\|\{(\d+)}~', $s, $matches)) {
        print_r($matches[1]);
    }
    

    See the regex demo and the regex graph:

    enter image description here

    PHP demo:

    $s = '{1|{1078} {*|{1079}-}test{1|{4829}, test2 {4457}}} {*|{1078} {*|{1079}-}test3{1|{4829}, test4 {23232}}}';
    if (preg_match_all('~\|\{(\d+)}~', $s, $matches)) {
        print_r(array_unique($matches[1]));
    }
    // => Array ( [0] => 1078 [1] => 1079  [2] => 4829 )
    

    NOTE: array_unique will keep unique values only in the results.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么