duangongqiong6958 2009-12-04 05:59
浏览 12
已采纳

在php中解析iCalendar字符串的最佳方法

I am trying to write a class that can parse an iCalendar file and am hitting some brick walls. Each line can be in the format:

PARAMETER[;PARAM_PROPERTY..]:VALUE[,VALUE2..]

It's pretty easy to parse with either a bunch of splits or regex's until you find out that values can have backticked commas, also they can be double quote marked which makes life hard. for example:

PARAMETER:"my , cool, value",value\,2,value3

In this example you are meant to pull out the three values:

  • my , cool value
  • value,2
  • value3

Which makes it a little more difficult.

Suggestions?

  • 写回答

4条回答 默认 最新

  • douzhuan1169 2009-12-25 10:13
    关注

    Go through the file char by char and split the values manually, whenever you have a quotation mark you enter "quotation mode" where you won't split at commas and when the closing quotation mark comes you leave it.

    For the backticked commas: If you read in a backslash you also read the next character and decide what to do with it then.

    Of course that's not extremely efficient, but you can't use regular expressions for this. I mean you can, but since I believe that there also can be escaped quotation marks this is going to be really messy.

    If you want to give it a try though:

    • let's start by matching a quotation mark followed by characters that are not: "[^"]*"
    • to overcome the problem of escaped characters you can use lookaheads (?<!\\)"[^"]*(?<!\\)"
    • now it will break if escaped quotation marks are in the value, maybe this works?(haven't tested it) (?<!\\)"[^"|(?<=\\)"]*(?<!\\)"

    So you see it very fast get's messy, so I would suggest to you to read it in characterwise.

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?