简体   繁体   中英

How to use regular expression to extract *string* from an expression string?

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.

"((?:\\"|[^"])+)"

首先匹配\\“,然后匹配任何非引号的字符串。group(1)是内部字符串。

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM