要利用正则表达式提取成对出现的双引号或单引号中间的内容,可以使用以下的正则表达式:(['"])(.*?)\1

这个正则表达式的含义是:

  • (['"]):匹配一个单引号或双引号,并将其捕获到第一个捕获组中。
  • (.*?):非贪婪地匹配任意字符,0次或多次,将其捕获到第二个捕获组中。
  • \1:引用第一个捕获组,确保结束引号与开始引号相同。

以下是一个示例的Java代码,演示如何使用正则表达式提取成对出现的引号中间的内容:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class QuoteExtractor {
    public static void main(String[] args) {
        String input = "This is a 'test' string with 'quoted' words.";
        String regex = "(['\"])(.*?)\\1";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            String match = matcher.group(2);
            System.out.println(match);
        }
    }
}

运行以上代码,输出结果为:

test
quoted

这个例子中,我们使用正则表达式(['"])(.*?)\1在字符串"This is a 'test' string with 'quoted' words."中提取出了成对出现的引号中间的内容

java 利用正则表达式提取成对出现的双引号或单引号中间的内容不是成对的不提取1那种写法

原文地址: http://www.cveoy.top/t/topic/ib2g 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录