Annie
Annie

Reputation: 3190

Regex for newline

I am trying to replace the > character newline, but unable to figure out the regex part.

I want:

"<StartTag>"

To be replaced with:

"<StartTag>\n"

but NOT:

"</EndTag>\n"

When I use tags_string.Replace(">", "\n"), it replaces both.

Can anyone please help with Regex, so I can use Regex.Replace() instead, to handle the EndTag case?

Upvotes: 3

Views: 153

Answers (2)

gpmurthy
gpmurthy

Reputation: 2427

Consider...

string output = Regex.Replace(input, @"(?<=\<\w*)>", @">\n");

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213223

You can use the following pattern to match the opening tag:

(<[^/][^>]*>)

Then replace it with $1\n.

Regex.Replace(yourText, @"(<[^/][^>]*>)", "$1\n");

Upvotes: 3

Related Questions