duankuai6586 2014-05-18 17:17
浏览 25
已采纳

复杂的正则表达式场景

I have a scenario where I have multiple items with similar titles and need to simplify the titles so that they each have an identical manifest title.

Here are possible variations:

   Quarter Final Football vs. Loyola #2 -- 46:00
   Quarter Final Football vs. Loyola #1 -- 32:00
   Little Shop Act II -- 39:30 -- 5/22/91
   Little Shop Act I -- 56:15 -- 5/22/91
   B-BALL -- 3/14/86 -- vs. King -- First Half -- 43:15
   B-BALL -- 3/14/86 -- vs. King -- Second Half -- 48:15
   Oak Park 9/26/70 Part 1
   Oak Park 9/26/70 Part 2
   vs. Yale (1958) First Period
   vs. Yale (1958) Second Period

The matching result would ideally be "vs. Yale (1958)" for the bottom two. I tried with the bottom string with this regex pattern, but am feeling a bit lost as to how to handle all these variations programatically.

([.,a-zA-Z0-9\s\(\)]*)\b([#]|Period|period)\b

Thanks for any direction or help.

Edited: Output for each variation-

Quarter Final Football vs. Loyola
Quarter Final Football vs. Loyola
Little Shop
Little Shop
B-BALL -- 3/14/86 -- vs. King
B-BALL -- 3/14/86 -- vs. King
Oak Park 9/26/70
Oak Park 9/26/70
vs. Yale (1958)
vs. Yale (1958)
  • 写回答

1条回答 默认 最新

  • doubleyou1001 2014-05-18 22:42
    关注

    There does not seem to be a common denominator. If that is the case, you will need a list.

    I think you need to consider what might be the start of the various phrases that would be considered surperfluous. The following regex works on your examples; but whether it can be generalized to your real data remains to be seen. I used alternation. If there can be more than two, you might need to add Third, Fourth, etc to the list.

    \s*(?:#|act|(?:--)?\s*(?:First|Second)|Part).*
    

    Used perhaps something like:

    $result = preg_replace('/\s*(?:#|act|(?:--)?\s*(?:First|Second)|Part).*/i', '', $subject);
    

    Note that I used the case-insensitive mode; and you also want dot to Not match newline.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?