在 C# 中匹配换行符 (\n) 的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1175053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex that matches a newline (\n) in C#
提问by Dabblernl
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 will display like:
(old text)
new text
newContent 将显示为:(
旧文本)
新文本
I need a regular expression that strips away the text between parentheses with the parenthesis included AND the newline character.
我需要一个正则表达式,去掉括号之间的文本,其中包含括号和换行符。
The best I can come up with is:
我能想到的最好的是:
const string regex = @"^(\(.*\)\s)?(?<capture>.*)";
var match= Regex.Match(original_content, regex);
var stripped_content = match.Groups["capture"].Value;
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.
这工作,但我想专门匹配换行符(\n
),没有任何空格(\s
)替换\s
用\n
\\n
或\\\n
无法正常工作。
Please help me hold on to my sanity!
请帮助我保持理智!
EDIT: an example:
编辑:一个例子:
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;
}
Reply("(messageOne)\nmessageTwo","messageThree") returns :
(messageTwo)
messageThree
采纳答案by Jon Grant
If you specify RegexOptions.Multiline then you can use ^
and $
to match the start and end of a line, respectively.
如果指定 RegexOptions.Multiline,则可以使用^
和$
分别匹配行的开头和结尾。
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)
.
如果您不想使用此选项,请记住,新行可能是以下任何一项:\n
, \r
, \r\n
,因此\n
您不应只查找,而应该使用类似的内容:[\n\r]+
或更确切地说:(\n|\r|\r\n)
。
回答by spender
If you're trying to match line endings then you may find
如果您尝试匹配行尾,那么您可能会发现
Regex.Match("string", "regex", RegexOptions.Multiline)
helps
帮助
回答by JP Alioto
You are probably going to have a \r before your \n. Try replacing the \s with (\r\n).
您可能会在 \n 之前有一个 \r。尝试用 (\r\n) 替换 \s。
回答by Saulius
Actually it works but with opposite option i.e.
实际上它有效,但有相反的选择,即
RegexOptions.Singleline
回答by Najeeb
Think I may be a bit late to the party, but still hope this helps.
想我参加聚会可能有点晚了,但仍然希望这会有所帮助。
I needed to get multiple tokens between two hash signs.
我需要在两个哈希符号之间获取多个令牌。
Example i/p:
示例 i/p:
## token1 ##
## token2 ##
## token3_a
token3_b
token3_c ##
## token1 ##
## token2 ##
## token3_a
token3_b
token3_c ##
This seemed to work in my case:var matches = Regex.Matches (mytext, "##(.*?)##", RegexOptions.Singleline);
Of course, you may want to replace the double hash signs at both ends with your own chars.
这在我的情况下似乎有效:var matches = Regex.Matches (mytext, "##(.*?)##", RegexOptions.Singleline);
当然,您可能想用自己的字符替换两端的双井号。
HTH.
哈。
回答by WerWet
Counter-intuitive as it is, you could use both Multiline
and Singleline
option.
与直觉相反,您可以同时使用Multiline
和Singleline
选项。
Regex.Match(input, @"(.+)^(.*)", RegexOptions.Multiline | RegexOptions.Singleline)
First capturing group will contain first line (including \r
and \n
) and second group will have second line.
第一个捕获组将包含第一行(包括 \r
和\n
),第二个组将包含第二行。
Why:
为什么:
Multiline
:^
and$
match the beginning and end of each line (instead of the beginning and end of the input string).Singleline
:The period (
.
) matches every character (instead of every character except\n
)
Multiline
:^
并$
匹配每行的开头和结尾(而不是输入字符串的开头和结尾)。Singleline
:句点 (
.
) 匹配每个字符(而不是每个字符,除了\n
)