vCard 4.0 正则表达式

发布于 2024-12-17 12:54:45 字数 582 浏览 1 评论 0原文

不久前,我创建了一个程序来处理 vCard 文件。这实际上可以通过以下方式完美完成:

(?<FIELD>[^\s:;]+)(;(?<PARAM>[^:]+))*:(?<CONTENT>.*(?>\r\n[ \t].*)*)$

但是,这不适用于新的(2011 年 8 月)vCard 4.0 标准。问题是 vCard 4.0 文件使用以下布局:

FIELD(:)(;([PARAMETER]="[CONTENT],[MORE CONTENT]"(;))[DATATYPE(:)]:)CONTENT[newline]

例如,

ADR;type="home,work":(address)

如您所见,我想捕获整个参数,包括 type="..." 内容。

所以我的问题是:我的代码是否可以修改,或者我是否必须编写两个进程(一个用于旧类型,一个用于新的 4.0 版本;理想情况下,我想支持两者),如果可以,如何编写? (顺便说一句,我正在使用 c# 和 .net 4.0)。

问候。

a while ago, I created a program to process vCard files. This could be done virtually perfectly with the following:

(?<FIELD>[^\s:;]+)(;(?<PARAM>[^:]+))*:(?<CONTENT>.*(?>\r\n[ \t].*)*)$

However, this doesn't work for the new (August 2011) vCard 4.0 standard. The problem is that vCard 4.0 files use the following layout:

FIELD(:)(;([PARAMETER]="[CONTENT],[MORE CONTENT]"(;))[DATATYPE(:)]:)CONTENT[newline]

e.g.

ADR;type="home,work":(address)

As you can see, I would like to capture the whole parameter, including the type="..." stuff.

So my question is: can my code be modified or will I have to write two processes (one for the old types and one for the new 4.0 version; ideally, I would like to support both) and if so, how? (I'm using c# and .net 4.0 by the way).

Regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

电影里的梦 2024-12-24 12:54:45

尝试以下正则表达式:

(?<FIELD>[^\s:;]+)(;(?<PARAM>[^=:;]+)=\"?(?<VALUE>[^:;]+)\"?)*:(?<CONTENT>[^;]*;?)*

该查询似乎可以处理此处提供的 vCard 3.0 示例:

ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America

以及4.0 示例:

ADR;TYPE=work;LABEL="42 Plantation St.\nBaytown, LA 30314\nUnited States of America"
 :;;42 Plantation St.;Baytown;LA;30314;United States of America

它也与 vCard 4.0 规范 此处

ADR;GEO="geo:12.3457,78.910";LABEL="Mr. John Q. Public, Esq.\n
      Mail Drop: TNE QB\n123 Main Street\nAny Town, CA  91921-1234\n
      U.S.A.":;;123 Main Street;Any Town;CA;91921-1234;U.S.A.

我的免责声明是,我在 vCard 方面没有任何专业知识,我只是浏览了规范的一部分并在使用 RegExr 所以我可能错过了一些边缘情况。

Try the following regex:

(?<FIELD>[^\s:;]+)(;(?<PARAM>[^=:;]+)=\"?(?<VALUE>[^:;]+)\"?)*:(?<CONTENT>[^;]*;?)*

That query seems to handle both the vCard 3.0 example provided here:

ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America

And the 4.0 example:

ADR;TYPE=work;LABEL="42 Plantation St.\nBaytown, LA 30314\nUnited States of America"
 :;;42 Plantation St.;Baytown;LA;30314;United States of America

It also matches this example from the vCard 4.0 Specification here:

ADR;GEO="geo:12.3457,78.910";LABEL="Mr. John Q. Public, Esq.\n
      Mail Drop: TNE QB\n123 Main Street\nAny Town, CA  91921-1234\n
      U.S.A.":;;123 Main Street;Any Town;CA;91921-1234;U.S.A.

My disclaimer is that I don't have any expertise in vCard specifically, I just skimmed a portion of the spec and looked at examples while playing around with RegExr so it's possible that I'm missing some edge cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文