java - 用于匹配引号内模式的正则表达式

标签 java regex

我有一些输入数据,例如

some string with 'hello' inside 'and inside'

如何编写正则表达式,以便返回引用的文本(无论重复多少次)(所有出现的情况)。

我有一个返回单引号的代码,但我想让它返回多次出现:

String mydata = "some string with 'hello' inside 'and inside'";
Pattern pattern = Pattern.compile("'(.*?)+'");
Matcher matcher = pattern.matcher(mydata);
while (matcher.find())
{
    System.out.println(matcher.group());
}

最佳答案

查找我所有的事件:

String mydata = "some '' string with 'hello' inside 'and inside'";
Pattern pattern = Pattern.compile("'[^']*'");
Matcher matcher = pattern.matcher(mydata);
while(matcher.find())
{
    System.out.println(matcher.group());
}

输出:

''
'hello'
'and inside'

模式描述:

'          // start quoting text
[^']       // all characters not single quote
*          // 0 or infinite count of not quote characters
'          // end quote

关于java - 用于匹配引号内模式的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14833012/

相关文章:

java - 为什么我的笑脸 GUI 看起来是一种颜色,而不是所有元素都有自己单独的随机颜色?

java - 使用 thymeleaf、Spring Boot 获取复选框列表的值

java - 将消息从 Java 放入队列

regex - 使用 sed 将换行符转义符 '\n' 替换为转义的换行符转义符 '\\n'

java - mongodb 中的日期不正确

java - 存储数据的桌面应用程序?

使用自定义正则表达式替换 Java 字符串

javascript - 如果字符串不是较大字符串的一部分,则使用 javascript 中的正则表达式替换字符串

javascript - 如何使用正则表达式获取 url 中的路径

javascript - 将 Go Regexp 翻译成 Javascript