medium
medium

Reputation: 4236

get regex to work in java

I have this regular expression pattern,

From: ["<][^>]*>

I need it to work in java and the double quotes is producing an error. When I try and escape it like so

From: [\"<][^>]*>

it does not produce the correct result. Does anyone know how to handle double quotes in java for regular expressions? Thanks

Upvotes: 1

Views: 116

Answers (1)

maple_shaft
maple_shaft

Reputation: 10463

The \ character in Java String literals is a reserved escape character, so to add a regex escape character into a Java literal String object one must Escape the Escape :)

Eg. \\" will result in a regex of \" which will find double quote characters.

EDIT: One thing that I forgot was that the double quote character is also a reserved character for a Java string literalas well! Because of this the \ for the regex must be escaped as well as the " character.

The actual Java string literal will look like this String regex = "\\\"";

Upvotes: 5

Related Questions