正则表达式,在C#中匹配一个换行符(\ n)
确定,这个人是我发疯.... 我有一个是这样形成的字符串:
OK, this one is driving me nuts.... I have a string that is formed thus:
var newContent = string.Format("({0})\n{1}", stripped_content, reply)
newContent将提示:
(旧文本)
新文本
newContent will display like:
(old text)
new text
我需要一个正规的前pression认为除掉与包括括号和换行符括号之间的文本。
I need a regular expression that strips away the text between parentheses with the parenthesis included AND the newline character.
我能拿出最好的是:
const string regex = @"^(\(.*\)\s)?(?<capture>.*)";
var match= Regex.Match(original_content, regex);
var stripped_content = match.Groups["capture"].Value;
这工作,但我想专门匹配换行符( \ñ
),没有任何空白( \ S
)
更换 \ S
与 \ñ
\\ñ
或 \\\ñ
不能正常工作。
This works, but I want specifically to match the newline (\n
), not any whitespace (\s
)
Replacing \s
with \n
\\n
or \\\n
does NOT work.
请帮我坚持我的理智!
编辑:一个例子:
public string Reply(string old,string neww)
{
const string regex = @"^(\(.*\)\s)?(?<capture>.*)";
var match= Regex.Match(old, regex);
var stripped_content = match.Groups["capture"].Value;
var result= string.Format("({0})\n{1}", stripped_content, neww);
return result;
}
回复((MessageOne的)\ nmessageTwo,messageThree)返回:
(messageTwo)
messageThree
Reply("(messageOne)\nmessageTwo","messageThree") returns :
(messageTwo)
messageThree
如果您指定RegexOptions.Multiline那么你可以使用 ^
和 $
来一行的开始和结束相匹配,分别
If you specify RegexOptions.Multiline then you can use ^
and $
to match the start and end of a line, respectively.
如果您不希望使用此选项,请记住,一个新的生产线可能是以下中的任何一个: \ñ
, \ - [R
, \ r \ n个
,所以不是只为找\ñ
,你或许应该使用类似: [\ñ\ R] +
,或者更确切地说:(\ N | \ r | \ r \ n)的
。
If you don't wish to use this option, remember that a new line may be any one of the following: \n
, \r
, \r\n
, so instead of looking only for \n
, you should perhaps use something like: [\n\r]+
, or more exactly: (\n|\r|\r\n)
.