简体   繁体   English

正则表达式用于在引号内匹配模式

[英]Regex for matching pattern within quotes

I have some input data such as 我有一些输入数据,例如

some string with 'hello' inside 'and inside' 在“和内部”内部带有“ hello”的一些字符串

How can I write a regex so that the quoted text (no matter how many times it is repeated) is returned (all of the occurrences). 我如何编写一个正则表达式,以便返回引用的文本(无论重复多少次)(所有情况)。

I have a code that returns a single quotes, but I want to make it so that it returns multiple occurances: 我有一个返回单引号的代码,但我想使其返回多个事件:

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());
}

Find all occurences for me: 查找所有与我有关的事件:

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());
}

Output: 输出:

 '' 'hello' 'and inside' 

Pattern desciption: 模式描述:

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

我相信这应该符合您的要求:

\'\w+\'

\\'.*?' is the regex you are looking for. 是您要查找的正则表达式。

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

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