Alan2
Alan2

Reputation: 24572

How can I create a regular expression that includes a line feed?

I have this code:

Regex regex = new Regex(@"\[see=[^\]]*\]"); 

var list = phraseSources.ToList();

list.ForEach(item => item.JmdictMeaning = regex.Replace(item.JmdictMeaning, ""));

It removes all between "[see" and "]"

However I found out that before the "[see" is a line feed and I would like to remove this in addition to all between "[see" and "]".

I used a hex editor and I can see:

0D 0A 

Which I think is causing the line feed but I am not sure how to make a regex that includes this.

Can someone tell me how I can remove this plus all that comes between "[see" and "]"

Upvotes: 0

Views: 72

Answers (1)

dee-see
dee-see

Reputation: 24078

You can change your regex to also include carriage returns (0D, aka \r) and line feeds (0A, aka \n)

Regex regex = new Regex(@"\[see=[^\]]*\]|\r\n");

So now it matches anything that's [see=...] or \r\n.

If you expect either of them to appear by itself in the string you could also match something like this

Regex regex = new Regex(@"\[see=[^\]]*\]|[\r\n]");

It would clean up any \r and \n characters even if they appear alone.

If you meant that you want to only remove a \r\n that might appear before the [see=... part, then this would do

Regex regex = new Regex(@"(\r\n)?\[see=[^\]]*\]");

Upvotes: 1

Related Questions