Regular Expression for attribute/value not working in Java

I would like to avoid texts like this one: height="49" with a regular expression.

I tried .replaceAll("\s*="*"","");

(replaceAll is used as a method in a java class), but eclipse don't allowed me to do that. Any other suggestion??

Answers


You need to escape the \ in Java string literal to \\, and you need to escape the " to \".

.replaceAll("\\s*=\".*?\"","")
See also

The character and string escape sequences allow for the representation of some nongraphic characters as well as the single quote, double quote, and backslash characters in character literals (§3.10.4) and string literals (§3.10.5).

EscapeSequence:
        \ b                     /* \u0008: backspace BS                   */
        \ t                     /* \u0009: horizontal tab HT              */
        \ n                     /* \u000a: linefeed LF                    */
        \ f                     /* \u000c: form feed FF                   */
        \ r                     /* \u000d: carriage return CR             */
        \ "                     /* \u0022: double quote "                 */
        \ '                     /* \u0027: single quote '                 */
        \ \                     /* \u005c: backslash \                    */

You need to escape the backslash and the double quotes inside the string. Also you probably meant to write .* everywhere you wrote * (though that would likely not give you the results you want either, so you should use something more specific than .).


This needs a good deal more clarification, but your specific example can be found using

String.replaceAll("[A-Za-z]+=\"\d+\"","")

Need Your Help

Getting a poster frame(thumbnail) with ffmpeg

c# ffmpeg screenshot

I am trying to get a poster frame from a video file, using ffmpeg.

How to validate form fields with javascript objects?

javascript jquery validation

I am trying to do some simple form validation using javascript object values. I know it's not "ideal", but I'm just working with a simple form that doesn't need to be iron-clad.