Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 2个字符串之间的字符串的正则表达式c#_Regex - Fatal编程技术网

Regex 2个字符串之间的字符串的正则表达式c#

Regex 2个字符串之间的字符串的正则表达式c#,regex,Regex,我正在尝试从这个VMG文件中获取消息字符串。我只想在日期行之后和“END:VBODY”之前添加字符串 到目前为止,我得到的最好的正则表达式字符串是BEGIN:VBODY([^\n]*\n+)+END:VBODY 有人能帮忙改进吗 N: TEL:+65123345 END:VCARD BEGIN:VENV BEGIN:VBODY Date:8/11/2013 11:59:00 PM thi is a test message Hello this is a test message on line

我正在尝试从这个VMG文件中获取消息字符串。我只想在日期行之后和“END:VBODY”之前添加字符串

到目前为止,我得到的最好的正则表达式字符串是BEGIN:VBODY([^\n]*\n+)+END:VBODY

有人能帮忙改进吗

N:
TEL:+65123345
END:VCARD
BEGIN:VENV
BEGIN:VBODY
Date:8/11/2013 11:59:00 PM
thi is a test message
Hello this is a test message on line 2
END:VBODY
END:VENV
END:VENV
END:VMSG

如果你想使用正则表达式,你可以稍微修改一下你当前的正则表达式,因为$0组有你想要的

BEGIN:VBODY\n?((?:[^\n]*\n+)+?)END:VBODY
基本上,所发生的事情是将
([^\n]*\n+)
变成
(?:[^\n]*\n+?
(将此部分变为惰性可能更安全)

然后把整个部分都包起来:
((?[^\n]*\n+)

我在此之前添加了
\n?
,以使输出更清晰


非正则表达式解决方案可能是这样的:

string str = @"N:
    TEL:+65123345
    END:VCARD
    BEGIN:VENV
    BEGIN:VBODY
    Date:8/11/2013 11:59:00 PM
    thi is a test message
    Hello this is a test message on line 2
    END:VBODY
    END:VENV
    END:VENV
    END:VMSG";

int startId = str.IndexOf("BEGIN:VBODY")+11; // 11 is the length of "BEGIN:VBODY"
int endId = str.IndexOf("END:VBODY");
string result = str.Substring(startId, endId-startId);
Console.WriteLine(result);
输出:

Date:8/11/2013 11:59:00 PM
thi is a test message
Hello this is a test message on line 2

如果你想使用正则表达式,你可以稍微修改一下你当前的正则表达式,因为$0组有你想要的

BEGIN:VBODY\n?((?:[^\n]*\n+)+?)END:VBODY
基本上,所发生的事情是将
([^\n]*\n+)
变成
(?:[^\n]*\n+?
(将此部分变为惰性可能更安全)

然后把整个部分都包起来:
((?[^\n]*\n+)

我在此之前添加了
\n?
,以使输出更清晰


非正则表达式解决方案可能是这样的:

string str = @"N:
    TEL:+65123345
    END:VCARD
    BEGIN:VENV
    BEGIN:VBODY
    Date:8/11/2013 11:59:00 PM
    thi is a test message
    Hello this is a test message on line 2
    END:VBODY
    END:VENV
    END:VENV
    END:VMSG";

int startId = str.IndexOf("BEGIN:VBODY")+11; // 11 is the length of "BEGIN:VBODY"
int endId = str.IndexOf("END:VBODY");
string result = str.Substring(startId, endId-startId);
Console.WriteLine(result);
输出:

Date:8/11/2013 11:59:00 PM
thi is a test message
Hello this is a test message on line 2

下面是一个使用正则表达式的解决方案

        string text = @"N:
        TEL:+65123345
        END:VCARD
        BEGIN:VENV
        BEGIN:VBODY
        Date:8/11/2013 11:59:00 PM
        thi is a test message
        Hello this is a test message on line 2
        END:VBODY
        END:VENV
        END:VENV
        END:VMSG";


string pattern = @"BEGIN:VBODY(?<Value>[a-zA-Z0-9\r\n.\S\s ]*)END:VBODY";//Pattern to match text.
Regex rgx = new Regex(pattern, RegexOptions.Multiline);//Initialize a new Regex class with the above pattern.
Match match = rgx.Match(text);//Capture any matches.
if (match.Success)//If a match is found.
{
        string value2 = match.Groups["Value"].Value;//Capture match value.
        MessageBox.Show(value2);
}
演示


希望有帮助。

这里有一个使用正则表达式的解决方案

        string text = @"N:
        TEL:+65123345
        END:VCARD
        BEGIN:VENV
        BEGIN:VBODY
        Date:8/11/2013 11:59:00 PM
        thi is a test message
        Hello this is a test message on line 2
        END:VBODY
        END:VENV
        END:VENV
        END:VMSG";


string pattern = @"BEGIN:VBODY(?<Value>[a-zA-Z0-9\r\n.\S\s ]*)END:VBODY";//Pattern to match text.
Regex rgx = new Regex(pattern, RegexOptions.Multiline);//Initialize a new Regex class with the above pattern.
Match match = rgx.Match(text);//Capture any matches.
if (match.Success)//If a match is found.
{
        string value2 = match.Groups["Value"].Value;//Capture match value.
        MessageBox.Show(value2);
}
演示


希望有帮助。

我认为您不需要使用正则表达式。在BEGIN之后收集任何行要简单得多:VBODY会一直看到,直到行结束:VBODYHey Spaceghost,你知道一个示例c#代码看起来像什么吗?我认为你不需要使用正则表达式。在BEGIN之后收集任何行要简单得多:VBODY会一直看到,直到行结束:VBODYHey Spaceghost,你知道这个示例c#代码是什么样子吗