Regex 用于在Perl中捕获vcard组的正则表达式

Regex 用于在Perl中捕获vcard组的正则表达式,regex,regex-lookarounds,regex-group,vala,regex-greedy,Regex,Regex Lookarounds,Regex Group,Vala,Regex Greedy,这学期我一直在大学学习语法和语义学,regex经常参与其中。作为一种练习方式,我发现可以应用正则表达式的不同场景。考虑到VCARD就是其中之一,我一直无法指定在BEGIN:VCARD和END:VCARD 请注意,.vcf文件使用行分隔 我的最佳模式看起来是这样的:(尽管我尝试了许多变化 BEGIN:VCARD\n([^(END:VCARD)\n]*END:VCARD 所以这个想法是:“从开始vcard读取所有不是结束的内容:vcard,并以换行符结束,直到遇到结束vcard” 我使用的是perl

这学期我一直在大学学习语法和语义学,regex经常参与其中。作为一种练习方式,我发现可以应用正则表达式的不同场景。考虑到VCARD就是其中之一,我一直无法指定在
BEGIN:VCARD
END:VCARD

请注意,.vcf文件使用行分隔

我的最佳模式看起来是这样的:(尽管我尝试了许多变化

BEGIN:VCARD\n([^(END:VCARD)\n]*END:VCARD

所以这个想法是:“从开始vcard读取所有不是结束的内容:vcard,并以换行符结束,直到遇到结束vcard”

我使用的是perl变体,但使用的是vala编程语言

我意识到问题在于我的模式,但经过长时间的阅读和反复尝试,我仍然不太确定为什么测试仪显示它不工作

测试数据:

BEGIN:VCARD
VERSION:3.0
N:Doe;John;;;
FN:John Doe
ORG:Example.com Inc.;
TITLE:Imaginary test person
EMAIL;type=INTERNET;type=WORK;type=pref:johnDoe@example.org
TEL;type=WORK;type=pref:+1 617 555 1212
TEL;type=WORK:+1 (617) 555-1234
TEL;type=CELL:+1 781 555 1212
TEL;type=HOME:+1 202 555 1212
NOTE:John Doe has a long and varied history\, being documented on more police files that anyone else. Reports of his death are alas numerous.
CATEGORIES:Work,Test group
X-ABUID:5AD380FD-B2DE-4261-BA99-DE1D1DB52FBE\:ABPerson
END:VCARD
BEGIN:VCARD
VERSION:3.0
N:Doe;Jane;;;
FN:Jane Doe
ORG:Example.com Inc.;
TITLE:Another Imaginary test person
EMAIL;type=INTERNET;type=WORK;type=pref:johnDoe@example.org
TEL;type=WORK;type=pref:+1 617 555 1213
TEL;type=WORK:+1 (617) 555-1233
TEL;type=CELL:+1 781 555 1213
TEL;type=HOME:+1 202 555 1213
NOTE:Jane Doe has a long and varied history\, being documented on more police files that anyone else. Reports of her death are alas numerous.
CATEGORIES:Work,Test group
X-ABUID:5AD380FD-B2DE-4261-BA99-DE1D1DB52FBE\:ABPerson
END:VCARD

在我最成功的测试中,它标记了从第一个
BEGIN:VCARD
END:VCARD
前一行的所有内容。此表达式可能会帮助您:

(BEGIN:VCARD([\s\S]*?)END:VCARD)
Perl测试: 正则表达式 如果这不是您想要的表达式,您可以在中修改/更改表达式

正则表达式电路 您还可以在以下位置可视化您的表达式:


您应该使用
BEGIN:VCARD[\w\w]+?END:VCARD
这个正则表达式是否适用于它前面的内容或后面的内容?这个正则表达式开始按
BEGIN:VCARD
[\w\w]逐字匹配文本+?
使用
+
匹配任何字符,包括一个或多个换行符,
使捕获不贪婪,然后进行
END:VCARD
Thank you@PushpeshKumarRajwanshi的文字匹配
use strict;

my $str = 'BEGIN:VCARD
VERSION:3.0
N:Doe;John;;;
FN:John Doe
ORG:Example.com Inc.;
TITLE:Imaginary test person
EMAIL;type=INTERNET;type=WORK;type=pref:johnDoe@example.org
TEL;type=WORK;type=pref:+1 617 555 1212
TEL;type=WORK:+1 (617) 555-1234
TEL;type=CELL:+1 781 555 1212
TEL;type=HOME:+1 202 555 1212
NOTE:John Doe has a long and varied history\\, being documented on more police files that anyone else. Reports of his death are alas numerous.
CATEGORIES:Work,Test group
X-ABUID:5AD380FD-B2DE-4261-BA99-DE1D1DB52FBE\\:ABPerson
END:VCARD
BEGIN:VCARD
VERSION:3.0
N:Doe;Jane;;;
FN:Jane Doe
ORG:Example.com Inc.;
TITLE:Another Imaginary test person
EMAIL;type=INTERNET;type=WORK;type=pref:johnDoe@example.org
TEL;type=WORK;type=pref:+1 617 555 1213
TEL;type=WORK:+1 (617) 555-1233
TEL;type=CELL:+1 781 555 1213
TEL;type=HOME:+1 202 555 1213
NOTE:Jane Doe has a long and varied history\\, being documented on more police files that anyone else. Reports of her death are alas numerous.
CATEGORIES:Work,Test group
X-ABUID:5AD380FD-B2DE-4261-BA99-DE1D1DB52FBE\\:ABPerson
END:VCARD';
my $regex = qr/(BEGIN:VCARD([\s\S]*?)END:VCARD)/mp;

if ( $str =~ /$regex/g ) {
  print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
  # print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
  # print "Capture Group 2 is $2 ... and so on\n";
}

# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}