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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建