正则表达式寻找一个字 [英] Regex pattern to look for a word

查看:159
本文介绍了正则表达式寻找一个字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的正则表达式模式2换行符后删除所有字符

I have the following Regex pattern to remove all characters after the 2 line breaks.

(?<=.+[\r\n]+.+[\r\n]+)([\s\S]*)

在这里,我的问题是,我也想要一个2换行后添加检查特定的文字,例如,如果被发现,不包括它。

My problem here is that I also wanted to add a check for a specific text, for example after that 2 line breaks and if it is found, do not include it.

这是我做我的C#代码:

And here is how I do it on my c# code:

string newComment = string.IsNullOrEmpty(regexPattern) ? emailBody : new Regex(regexPattern, RegexOptions.IgnoreCase).Replace(emailBody, string.Empty);

修改

我想寻找一个特定的文字,例如这是一个标志:那么,如果它被发现,它不应该被包括在内,之后还什么,同时保持目前的设计都经过2换行符不会这被列入

I wanted to look for a specific text, for example "This is a signature:" then if it is found, it should not be included and anything after it also, while maintaining the current design which everything after 2 line breaks will not be included

样品字符串:

string body = "Try comment.";
string additionalBody = "This is a signature";
string newBody = body + System.Environment.NewLine + additionalBody + System.Environment.NewLine + "asd Asd";



所以 newBody 应导致3段。文

这应该会显示试评仅

可能出现的情况可能是:

Possible scenarios may be:

1)在第一或第二段,文本可以存在,并应被自动删除。

1) On the first or second paragraph, the text can be present and should be removed automatically.

2)如,自动签名是不存在的,但有3段,删除最后一个段落

2) If the automated signature is not present but there is 3 paragraphs, remove the last paragraph.

推荐答案

如何简单:

(?<=(?:.+[\r\n]+){2})([\s\S]*)This is a signature

这篇关于正则表达式寻找一个字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆