简体   繁体   English

如何使用正则表达式从表达式字符串中提取* string *?

[英]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." 我想使用一个正则表达式提取“ abc \\” abc \\“ abc” ,该正则表达式理解“反斜杠后的双引号不是字符串的结尾。” 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: 你们给了我一些提示,最后我弄清楚了,这是我的Java代码:

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. 我在Rad Software的Expression Designer中尝试了@PaulPRO的答案,但对我而言,它不适用于您的字符串。 This worked on your input using the tool I mentioned above. 使用上面提到的工具,这可以解决您的输入问题。

\".+?(\\|\"){1}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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