从VCARD读取元素

本文关键字:元素 读取 VCARD | 更新日期: 2023-09-27 17:53:55

我只是想从下面读取一个元素regex

 BEGIN:VCARD'nVERSION:2.1FN:Peter StoddartADR:My School (BranchName)TEL;CELL:00129222273645EMAIL;HOME;INTERNET:abc@gmail.comREV:3757END:VCARD

和我只想要值REV:3757

我尝试了多个正则表达式,但没有成功

'BREV'w*'b

返回REV:3757END,但我只想要3757

从VCARD读取元素

你可以使用遍历:

string text = "BEGIN:VCARD'nVERSION:2.1FN:Peter StoddartADR:My School (BranchName)TEL;CELL:00129222273645EMAIL;HOME;INTERNET:abc@gmail.comREV:3757END:VCARD";
var reg = new Regex(@"(?<='BREV:)'d+(?=END)");
var match = reg.Match(text);
Console.WriteLine(match);

ideone演示

或者如果你不介意使用捕获组:

string text = "BEGIN:VCARD'nVERSION:2.1FN:Peter StoddartADR:My School (BranchName)TEL;CELL:00129222273645EMAIL;HOME;INTERNET:abc@gmail.comREV:3757END:VCARD";
var reg = new Regex(@"'BREV:('d+)END");
var match = reg.Match(text);
Console.WriteLine(match.Groups[1].Value);

ideone演示

你试过了吗:

REV':'d+

这将只给你REV:3757

现在你可以使用负向前看给出数字的值。

(?<=REV:)'d+

这将只给您数字3757

这为我返回了修订。试一试。

public class Program
{
    const string input = "BEGIN:VCARD'nVERSION:2.1FN:Peter StoddartADR:My School (BranchName)TEL;CELL:00129222273645EMAIL;HOME;INTERNET:abc@gmail.comREV:3757END:VCARD";        
    public static void Main(string[] args)
    {
        Regex expression = new Regex(@"REV:(?<SearchID>['d]+)END");
        var match = expression.Match(input);
        if (match.Success)
        {
            Console.WriteLine(match.Groups["SearchID"].Value);
        }
    }
}

试试这个:

string result = Regex.Replace(searchText, "(.+''BREV''w*''b:([''d]+).+)", "$2");