Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Regexp replace \n

Tags:

c#

regex

I'm trying to remove any new line characters after [/quote]

I have this currently:

Comment = Regex.Replace(Comment, @"[/quote](\n){1,}", "[/quote]");

But it doesn't seem to do anything!

Example:

[/quote]


hey nice quote blah blah

Goes to

[/quote]hey nice quote blah blah
like image 470
Tom Gullen Avatar asked Feb 12 '26 20:02

Tom Gullen


1 Answers

Are you sure your string ends with \n (UNIX-style line ending), and not \r\n (Windows-style line ending)?

Also, realize that [...] in a regex indicates a character class, so your [/quote] matches a single character that is either /, q, u, o, t, or e. You have to escape the [ as \[ to match an open-bracket character.

Put them together (and simplify {1,} to the shorthand +), and try this:

Regex.Replace(Comment, @"\[/quote\][\r\n]+", "[/quote]");
like image 101
Joe White Avatar answered Feb 14 '26 18:02

Joe White



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!