如何使用C#在记事本中的两行之间查找文本 [英] How to find text between two lines in Notepad using C#
问题描述
我需要找到 - = _ NextPart_SMP_之间的所有行,使用c#
- = _ NextPart_SMP_1d0087c4c718772_aee00768_00000001 >
内容类型:image / jpeg
内容传输编码:base64
内容 - 位置:screenshot_0001.jpeg
/ 9J / 4AAQSkZJRgABAQEAYABgAAD / 2wBDABkRExYTEBkWFBYcGxkeJT4pJSIiJUw3Oi0 + WlBfXllQ
V1ZkcJB6ZGqIbFZXfap + iJSZoaKhYXiwva + cu5CeoZr / 2wBDARscHCUhJUkpKUmaZ1dnmpqampqa
mpqampqampqampqampqampqampqampqampqampqampqampqampqampqampr / wAARCAQABQADASIA
AhEBAxEB / 8QAHwAAAQUBAQEBAQEAAAAAAAAAAAAAAAQAQQAcAQAQA / 8QAtRAAAgA如果需要,可以使用一些可用的库来解析邮件消息。我推荐开源OpenPop.NET:
http://sourceforge.net/projects/hpop [ ^ ]。
这个库编写得不是很好,但它完全适用于所有这些MIME标准,并在代码中包含一个POP3客户端。你可以直接使用它,或者只是用作参考,这也很有用。
>
另一种方法是使用String.Split:private string [] splitString = new string [] { @ - = _ NextPart_SMP _};
private List< string> getSections( string source)
{
return source.Split(splitString,StringSplitOptions .RemoveEmptyEntries).ToList();
}
// 在方法中测试正文
私人 字符串 source = @ 您的测试内容在这里;
List< string> result = getSections(source);
if (result == null ) throw new EvaluateException( 无法解析来源!);
for ( int i = 0 ; i < result.Count; i ++)
{
Console.WriteLine( Section:{0}值:{1},i.ToString(),result [i]);
}
请看下面的例子。
string pattern = - = _ NextPart_SMP(?< result>。*) - = _ NextPart_SMP;
正则表达式正则表达式= 新正则表达式(模式);
匹配匹配= regex.Match( - = _ NextPart_SMP_1d0087c4c718772_aee00768_00000001 - = _ NextPart_SMP< /跨度>);
if (match.Success)
{
string result = match.Groups [ RESULT]。值;
}
这将给你_1d0087c4c718772_aee00768_00000001
祝你好运,
Stephan
I need to find all the lines between "--=_NextPart_SMP_" using c#
--=_NextPart_SMP_1d0087c4c718772_aee00768_00000001
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-Location: screenshot_0001.jpeg
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDABkRExYTEBkWFBYcGxkeJT4pJSIiJUw3Oi0+WlBfXllQ
V1ZkcJB6ZGqIbFZXfap+iJSZoaKhYXiwva+cu5CeoZr/2wBDARscHCUhJUkpKUmaZ1dnmpqampqa
mpqampqampqampqampqampqampqampqampqampqampqampqampqampqampr/wAARCAQABQADASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
--=_NextPart_SMP_1d0087c4c718772_aee00768_00000001
If you want, you can use some available library to parse mail messages. I would recommend open-source OpenPop.NET:
http://sourceforge.net/projects/hpop[^].
This library is not perfectly well written, but it thoroughly works with all those MIME standards and contains a POP3 client in the code. You can use it directly, or simply use as a reference, which is also useful.
—SA
Another alternative is to use String.Split:private string[] splitString = new string[]{@"--=_NextPart_SMP_"}; private List<string> getSections(string source) { return source.Split(splitString, StringSplitOptions.RemoveEmptyEntries).ToList(); } // test in a method body private string source = @"Your test content goes here"; List<string> result = getSections(source); if(result == null) throw new EvaluateException("can't parse source !"); for (int i = 0; i < result.Count; i++) { Console.WriteLine("Section: {0} Value: {1}",i.ToString(),result[i]); }
Hi,
please have a look at the following example.
string pattern = "--=_NextPart_SMP(?<result>.*)--=_NextPart_SMP"; Regex regex = new Regex(pattern); Match match = regex.Match("--=_NextPart_SMP_1d0087c4c718772_aee00768_00000001--=_NextPart_SMP"); if(match.Success) { string result = match.Groups["RESULT"].Value; }
This will give you "_1d0087c4c718772_aee00768_00000001"
Best regards,
Stephan
这篇关于如何使用C#在记事本中的两行之间查找文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!