tags:

views:

222

answers:

5

I am looking for a regular expression to filter out all \r\n out of the html file but if there is a textarea it should be passed without having the enter removed.

I am using .NET (C#) technology.

+3  A: 

Don't use regular expressions - use an HTML parser.

Mark Byers
avoid parsing ̣̉́̃HṬ̣̣́̀ML with ̣̃̉̉̀ŕ̃̃̃̉̀̀ẽ̉̉́̃g̣̣̃̃̃ẹ̣x̣̣̣̉̉̃̃?
aehiilrs
A: 

Alternative approach:

  1. Find, with regex, the position (in the string) where there's a textarea element. The suitable regex for this would be: (<textarea>(.*?)</textarea>)
  2. Remove the \r\n characters from everywhere, except the places you found on #1.
Dor
The problem with this is that the <textarea> tag will probably have attributes such as id, class, any custom attribs, etc, that may appear in any order, and may or may not be quoted correctly, among other issues. An HTML parser is the only way to get a reliable match.
David Lively
Do you have a sample on how to accomplish this?
Nyla Pareska
I built a regex that could find the location of something. `Regexp.new('[\s]*<img[\s]*src="http://imgs\.xkcd\.com/comics/[.]*')` it was to find the img tag of an xkcd comic, but it can be easily adapted for a dumb <textarea> finder.. Course, it's dumb so it doesn't recognize to ignore comments and other such things. I say HTML parser FTW though
Earlz
A: 

This is extremely similar to this answer I've given before.

Fortunately, .NET has a balanced matching feature.

So you can do this:

(<textarea[^>]*>[^<>]*(((?<Open><)[^<>]*)+((?<Close-Open>>)[^<>]*)+)*(?(Open)(?!))</textarea>)|\r\n

Then you can perform a replace value of $1.

Here it is in action: http://regexhero.net/tester/?id=292c5529-5fe8-42e9-8d72-d7ea9ab9e1fe

Hope that helps. The benefit of using balanced matching like this is that it's powerful enough to handle nested tags that are inherent to HTML.

However, it's still not 100% reliable. Comments can still throw it off. And of course this is also an insanely complicated regular expression to manage if you ever need to make changes. So you may still want to use an html parser after all.

Steve Wortham
Even with nested matching, regex is not a powerful enough tool to parse HTML correctly. Things like `<!-- <a> -->` will cause problems.
Mark Byers
Good point, I changed the last paragraph. I think this is about as reliable as you can make a regular expression for this task. But you're right -- an ill-placed comment can throw it off.
Steve Wortham
+2  A: 

Speaking of HTML parsers, the Html Agility Pack is great for solving this type of problem.

David Lively
A: 

Read this: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454

This question is like saying how do you do up a bolt with a hammer. Now I'm sure if you were determined enough you could do tighten the bolt with a hammer. However it would be difficult and problematic to say the least and the chances are you would break something by trying.

Take a step back, throw away the assumption that your hammer is the best tool and go back to your tool box, if you dig around in there you will find a better tool its called an HTML parser.

trampster