I have an expression string as below (whole line as a string):
String s = prefix + "abc\"abc\"abc".toUpperCase();
I want to extract "abc\"abc\"abc" using a regular expression which understands "double quotes after a backslash is not the end of the string." How can I make it? Thank you very much!
FINALLY
You guys gave me some hints and finally I figured it out and, this is my Java code:
public class RegExpTest {
private static final Pattern PATTERN = Pattern.compile("(([^\\\\]|^)\").*?([^\\\\]\")");
public static void main(String[] args) {
printStrings("He said \"Hello, \\\"\\\"\\\"\\\"name\\\"\", \"baby\"");
printStrings("\"Go away and \\\"never\\\" come back!\" he said.");
printStrings("\\\" outer \"inner\"");
}
private static void printStrings(String string) {
System.out.println(string);
System.out.println(extractStrings(string));
System.out.println();
}
private static List<String> extractStrings(String string) {
Matcher matcher = PATTERN.matcher(string);
List<String> resultList = new ArrayList<String>();
while (matcher.find()) {
String group = matcher.group();
if (!group.startsWith("\"")) {
group = group.substring(1); // remove first non-double-quoter
}
resultList.add(group);
}
return resultList;
}
}
It outputs as follows:
He said "Hello, \"\"\"\"name\"", "baby"
["Hello, \"\"\"\"name\"", "baby"]
"Go away and \"never\" come back!" he said.
["Go away and \"never\" 开发者_开发知识库come back!"]
\" outer "inner"
["inner"]
Thanks everyone.
You could use:
/".*?[^\]"/
All characters after the first "
until the next "
is reached which isn't preceded by a \
.
Note that this also will not match ""
. Since there must be at least one character between the quotes for it to match.
"((?:\\"|[^"])+)"
Would match \" first, then any non-quote string. group(1) is the inner string.
I tried @PaulPRO's answer in Rad Software's Expression designer but it didn't work on your string for me. This worked on your input using the tool I mentioned above.
\".+?(\\|\"){1}
精彩评论