First off, I understand that it is not ideal to parse html with regex.  However, I'm close to the solution I need, and I just can't quite get it right.  Say you have html input in a string and you do:  content = content.replaceAll("&lt;[^\\P{Graph}&gt;]+&gt;", "");   This will essentially remove html tags except those with non-printable characters, space, tab, newline, and control characters.  This is fine, except that there is a problem with the space character.  I need to replaceAll tags that look like:  &lt;ht ml&gt; (space somewhere in the middle)   but keep those that look like:  &lt; html&gt; (because this one contains a space as the FIRST character).   How can I adjust my regular expression for replaceAll() to accomplish this?  Thanks for any input/suggestions.
16095
Is there a good way to remove HTML from a Java string? A simple regex like replaceAll(&quot;\\&lt;.*?&gt;&quot;, &quot;&quot;)   will work, but things like &amp;amp; wont be converted correctly and non-HTML between the two angle brackets will be removed (i.e. the .*? in the regex will disappear).
78594
It is my understanding that the java.regex package does not have support for named groups () so can anyone point me towards a third-party library that does?  I've looked at  but its last release was in 2002 and it didn't work for me (admittedly I only tried briefly) under java5.
117699
How to represent space and tab in XML tag. Is there any special characters for them to represent.
37476
What is the easiest way to match non-ASCII characters in a regex? I would like to match all words individually in an input string, but the language may not be English, so I will need to match things like ü, ö, ß, and ñ. Also, this is in Javascript/jQuery, so any solution will need to apply to that.
6354
It seems like every question on stackoverflow where the asker is using regex to grab some information from HTML will inevitably have an "answer" that says not to use regex to parse HTML.  Why not? I'm aware that there are quote-unquote "real" HTML parsers out there like , and I'm sure they're powerful and useful, but if you're just doing something simple, quick, or dirty, then why bother using something so complicated when a few regex statements will work just fine?  Moreover, is there just something fundamental that I don't understand about regex that makes them a bad choice for parsing in general?
6954
I'm trying to split text in a JTextArea using a regex to split the String by \n However, this does not work and I also tried by \r\n|\r|n and many other combination of regexes. Code:  public void insertUpdate(DocumentEvent e) {     String split[], docStr = null;     Document textAreaDoc = (Document)e.getDocument();      try {         docStr = textAreaDoc.getText(textAreaDoc.getStartPosition().getOffset(), textAreaDoc.getEndPosition().getOffset());     } catch (BadLocationException e1) {         // TODO Auto-generated catch block         e1.printStackTrace();     }      split = docStr.split("\\n"); }
25338
I need to search a string and replace all occurrences of %FirstName% and %PolicyAmount% with a value pulled from a database. The problem is the capitalization of FirstName varies. That prevents me from using the String.Replace() method. I've seen web pages on the subject that suggest  Regex.Replace(strInput, strToken, strReplaceWith, RegexOptions.IgnoreCase);   However for some reason when I try and replace %PolicyAmount% with $0, the replacement never takes place. I assume that it has something to do with the dollar sign being a reserved character in regex.   Is there another method I can use that doesn't involve sanitizing the input to deal with regex special characters?
18762
I'd like to extract the text from an HTML file using Python.  I want essentially the same output I would get if I copied the text from a browser and pasted it into notepad.    I'd like something more robust than using regular expressions that may fail on poorly formed HTML.  I've seen many people recommend Beautiful Soup, but I've had a few problems using it.  For one, it picked up unwanted text, such as JavaScript source.  Also, it did not interpret HTML entities.  For example, I would expect &amp;#39; in HTML source to be converted to an apostrophe in text, just as if I'd pasted the browser content into notepad.  Update html2text looks promising. It handles HTML entities correctly and ignores JavaScript.  However, it does not exactly produce plain text; it produces markdown that would then have to be turned into plain text. It comes with no examples or documentation, but the code looks clean.    Related questions:
12603
I am using the function below to match URLs inside a given text and replace them for HTML links. The regular expression is working great, but currently I am only replacing the first match.  How I can replace all the URL? I guess I should be using the exec command, but I did not really figure how to do it.  function replaceURLWithHTMLLinks(text) {     var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&amp;@#\/%?=~_|!:,.;]*[-A-Z0-9+&amp;@#\/%=~_|])/i;     return text.replace(exp,"&lt;a href='$1'&gt;$1&lt;/a&gt;");  }
70124
Using regular expressions, what is the simplest way to fetch a websites HTML and find the value inside this tag (or any attribute's value for that matter):  &lt;html&gt;   &lt;head&gt;   [snip]   &lt;meta name="generator" value="thevalue i'm looking for" /&gt;   [snip]
20412
I was looking for a generic method in .Net to encode a string for use in an Xml element or attribute, and was surprised when I didn't immediately find one.  So, before I go too much further, could I just be missing the built-in function?    Assuming for a moment that it really doesn't exist, I'm putting together my own generic EncodeForXml(string data) method, and I'm thinking about the best way to do this.    The data I'm using that prompted this whole thing could contain bad characters like &amp;, &lt;, &quot;, etc.  It could also contains on occasion the properly escaped entities: &amp;amp;, &amp;lt;, and &amp;quot;, which means just using a CDATA section may not be the best idea.  That seems kinda klunky anyay; I'd much rather end up with a nice string value that can be used directly in the xml.  I've used a regular expression in the past to just catch bad ampersands, and I'm thinking of using it to catch them in this case as well as the first step, and then doing a simple replace for other characters.    So, could this be optimized further without making it too complex, and is there anything I'm missing? :  Function EncodeForXml(ByVal data As String) As String     Static badAmpersand As new Regex("&amp;(?![a-zA-Z]{2,6};|#[0-9]{2,4};)")      data = badAmpersand.Replace(data, "&amp;amp;")      return data.Replace("&lt;", "&amp;lt;").Replace("""", "&amp;quot;").Replace("&gt;", "gt;") End Function   Sorry for all you C# -only folks-- I don't really care which language I use, but I wanted to make the Regex static and you can't do that in C# without declaring it outside the method, so this will be VB.Net  Finally, we're still on .Net 2.0 where I work, but if someone could take the final product and turn it into an extension method for the string class, that'd be pretty cool too.  Update The first few responses indicate that .Net does indeed have built-in ways of doing this.  But now that I've started, I kind of want to finish my EncodeForXml() method just for the fun of it, so I'm still looking for ideas for improvement.  Notably: a more complete list of characters that should be encoded as entities (perhaps stored in a list/map), and something that gets better performance than doing a .Replace() on immutable strings in serial.
83703
I have an excel file that has cells with text that includes HTML tags.  The data was exported from a database and the HTML tags were included.  The HTML is stored as text, but I need it gone.  Is there a quick easy way to remove it all (other than find/replace)?
67152
I would like to create a page where all images which reside on my website are listed with title and alternative representation.  I already wrote me a little program to find and load all HTML files, but now I am stuck at how to extract src, title and alt from this HTML:  &lt;img src="/image/fluffybunny.jpg" title="Harvey the bunny" alt="a cute little fluffy bunny" />  I guess this should be done with some regex, but since the order of the tags may vary, and I need all of them, I don't really know how to parse this in an elegant way (I could do it the hard char by char way, but that's painful).
79970
I would like to use the .Net Regex.Split method to split this input string into an array. It must split on whitespace unless it is enclosed in a quote.  Input:  Here is "my string"&nbsp;&nbsp;&nbsp;&nbsp;it has "six&nbsp;&nbsp;matches"  Expected output:   Here is my string it has six&nbsp;&nbsp;matches   What pattern do I need? Also do I need to specify any RegexOptions?
62677
I am writing a Java utility which helps me to generate loads of data for performance testing.  It would be really cool to be able to specify a regex for Strings so that my generator spits out things which match this.  Is there something out there already baked which I can use to do this?  Or is there a library which gets me most of the way there?  Thanks
18554
I'm new to regular expressions and would appreciate your help.  I'm trying to put together an expression that will split the example string using all spaces that are not surrounded by single or double quotes. My last attempt looks like this: (?!") and isn't quite working. It's splitting on the space before the quote.  Example input:   This is a string that "will be" highlighted when your 'regular expression' matches something.   Desired output:  This is a string that will be highlighted when your regular expression matches something.   Note that "will be" and 'regular expression' retain the space between the words.
121366
I want to remove every two or more spaces between specific tags and leave just a space instead:  For example:  &lt;p class="text_obisnuit"&gt;  The context of articles,   stories, and conversations helps you     figure out and understand the meaning   of English words in the text that are new to you.   &lt;/p&gt;  My desire output:  &lt;p class="text_obisnuit"&gt;The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you.&lt;/p&gt;  I tried something but it did not work  (?&lt;=&lt;p class="text_obisnuit"&gt;)\s*|\s*(?=&lt;/p&gt;)