我只是想从下面的正则表达式中读取一个元素
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
答案 0 :(得分:1)
您可以使用外观:
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);
或者,如果您不介意使用捕获组:
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);
答案 1 :(得分:1)
REV\:\d+
这将只为您提供值REV:3757
现在你可以使用负向前瞻来给出数字的值。
(?<=REV:)\d+
这只会为您提供3757
答案 2 :(得分:1)
这为我返回了修订版。试一试。
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);
}
}
}
答案 3 :(得分:0)
试试这个:
string result = Regex.Replace(searchText, "(.+\\BREV\\w*\\b:([\\d]+).+)", "$2");