dsgd4654674 2014-12-18 13:02 采纳率: 0%
浏览 14
已采纳

变形字符串到模式

I'm working on an issue where users (truck drivers in this case) use SMS to send in information about work status. I want to keep the keying simple as not all users have smart phones so I have adopted some simple short codes for their input. Here are some examples and their meanings:

  • P#123456-3 (This is for picking up load 123456-3)
  • D#456789-1 (For the dropping of load 456789-1)
  • L#345678-9 (Load 345678-9 is going to be late)

This is pretty simple but users (and truck drivers) being what they are will key the updates in somewhat deviant manners such as:

  • #D 456789-1
  • D# 456789 - 1
  • D#.456789-1 This load looks wet to me do weneed to cancelthis order

You can pretty much come up with a dozen other permutations and it's not hard for me to catch and fix those that I can imagine.

I mostly use regular expressions to test the input against all my imagined "bad" patterns and then extract what I assume are the good parts, reassembling them into the correct order.

It's the new errors that cause me problems so I got to wondering if there was a more generic method where I can pass a "pattern" and a "message" to a function that would do it's best to turn the "message" into something matching the "pattern".

My searches have not found anything that really fits what I'm trying to do and I'm not even sure if there is a good general way to do this. I happen to be using PHP for this implementation but any type of example should help. Do any of you have a method?

  • 写回答

4条回答 默认 最新

  • dppxp79175 2014-12-18 14:04
    关注

    Try something like this:

    function parse($input) {
        // Clean up your input: 'D#.456789 - 1 foo bar' to 'D 456789 1 foo far'
        $clean = trim(preg_replace('/\W+/', ' ', $input));
        // Take first 3 words.
        list($status, $loadId1, $loadId2) = explode(' ', $clean);
        // Glue back your load ID to '456789-1'
        $loadId = $loadId1 . '-' . $loadId2;
        return compact('status', 'loadId');
    }
    

    Example:

    $inputs = array(
        'P#123456-3',
        '#D 456789-1',
        'D# 456789 - 1',
        'D#.456789-1 This load looks wet to me do weneed to cancelthis order',
    );
    echo '<pre>';
    foreach ($inputs as $s) {
        print_r(parse($s));
    }
    

    Output:

    Array
    (
        [status] => P
        [loadId] => 123456-3
    )
    Array
    (
        [status] => D
        [loadId] => 456789-1
    )
    Array
    (
        [status] => D
        [loadId] => 456789-1
    )
    Array
    (
        [status] => D
        [loadId] => 456789-1
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单