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

时间:2022-09-13 11:45:54

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

3 个解决方案

#1


3  

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

#2


0  

I believe this should fit your requirements:

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

\'\w+\'

#3


0  

\'.*?' is the regex you are looking for.

\ '*?'是你正在寻找的正则表达式。

#1


3  

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

#2


0  

I believe this should fit your requirements:

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

\'\w+\'

#3


0  

\'.*?' is the regex you are looking for.

\ '*?'是你正在寻找的正则表达式。