简体   繁体   English

在Java中使用正则表达式在双引号之间提取子字符串

[英]Extract a substring between double quotes with regular expression in Java

I have a string like this: 我有一个像这样的字符串:

"   @Test(groups = {G1}, description = "adc, def")"

I want to extract "adc, def" (without quotes) using regexp in Java, how should I do? 我想在Java中使用regexp提取“adc,def”(不带引号),我该怎么办?

If you really want to use regex: 如果你真的想使用正则表达式:

Pattern p = Pattern.compile(".*\\\"(.*)\\\".*");
Matcher m = p.matcher("your \"string\" here");
System.out.println(m.group(1));

Explanation: 说明:

.*   - anything
\\\" - quote (escaped)
(.*) - anything (captured)
\\\" - another quote
.*   - anything

However, it's a lot easier to not use regex: 但是,不使用正则表达式要容易得多:

"your \"string\" here".split("\"")[1]

Actually you'll get the IllegalStateException 实际上你会得到IllegalStateException

public class RegexDemo {
    public static void main(String[] args) {
        Pattern p = Pattern.compile(".*\\\"(.*)\\\".*");
        Matcher m = p.matcher("your \"string\" here");
        System.out.println(m.group(1));
    }
}

It gives: 它给:

Exception in thread "main" java.lang.IllegalStateException: No match found
    at java.util.regex.Matcher.group(Matcher.java:485)
    at RegexDemo.main(RegexDemo.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Before using group() , you need to call find() or matches() . 在使用group()之前,需要调用find()matches()

Simple test, for example: 简单测试,例如:

public class RegexTest {
    @Test(expected = IllegalStateException.class)
    public void testIllegalState() {
        String string = new String("your \"string\" here");
        Pattern pattern = Pattern.compile(".*\\\"(.*)\\\".*");
        Matcher matcher = pattern.matcher(string);

        System.out.println(matcher.group(1));
    }

    @Test
    public void testLegalState() {
        String string = new String("your \"string\" here");
        Pattern pattern = Pattern.compile(".*\\\"(.*)\\\".*");
        Matcher matcher = pattern.matcher(string);

        if(matcher.find()) {
            System.out.println(matcher.group(1));
        }
    }
}

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

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