如何使用regex提取子字符串

时间:2022-09-13 11:24:55

I have a string that has two single quotes in it, the ' character. In between the single quotes is the data I want.

我有一个字符串,它有两个单引号,字符。在单引号之间是我想要的数据。

How can I write a regex to extract "the data i want" from the following text?

如何编写regex从以下文本中提取“我想要的数据”?

mydata = "some string with 'the data i want' inside";

9 个解决方案

#1


431  

Assuming you want the part between single quotes, use this regular expression with a Matcher:

假设您需要单引号之间的部分,请使用Matcher的正则表达式:

"'(.*?)'"

Example:

例子:

String mydata = "some string with 'the data i want' inside";
Pattern pattern = Pattern.compile("'(.*?)'");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find())
{
    System.out.println(matcher.group(1));
}

Result:

结果:

the data i want

#2


52  

You don't need regex for this.

你不需要regex。

Add apache commons lang to your project (http://commons.apache.org/proper/commons-lang/), then use:

将apache commons lang添加到项目中(http://commons.apache.org/proper/commons-lang/),然后使用:

String dataYouWant = StringUtils.substringBetween(mydata, "'");

#3


9  

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

public class Test {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile(".*'([^']*)'.*");
        String mydata = "some string with 'the data i want' inside";

        Matcher matcher = pattern.matcher(mydata);
        if(matcher.matches()) {
            System.out.println(matcher.group(1));
        }

    }
}

#4


8  

Because you also ticked Scala, a solution without regex which easily deals with multiple quoted strings:

因为您还修改了Scala,一个没有regex的解决方案,可以轻松处理多个引号字符串:

val text = "some string with 'the data i want' inside 'and even more data'"
text.split("'").zipWithIndex.filter(_._2 % 2 != 0).map(_._1)

res: Array[java.lang.String] = Array(the data i want, and even more data)

#5


3  

There's a simple one-liner for this:

这里有一句简单的俏皮话:

String target = myData.replaceAll("[^']*(?:'(.*?)')?.*", "$1");

By making the matching group optional, this also caters for quotes not being found by returning a blank in that case.

通过使匹配组可选,还可以通过返回空格来满足没有找到的引号。

See live demo.

看到现场演示。

#6


3  

String da*t = mydata.replaceFirst(".*'(.*?)'.*", "$1");

#7


2  

as in javascript:

在javascript中:

mydata.match(/'([^']+)'/)[1]

the actual regexp is: /'([^']+)'/

实际的regexp:/([^]+)/

if you use the non greedy modifier (as per another post) it's like this:

如果你使用非贪婪修饰符(根据另一篇文章),它是这样的:

mydata.match(/'(.*?)'/)[1]

it is cleaner.

它是清洁。

#8


2  

In Scala,

在Scala中,

val ticks = "'([^']*)'".r

ticks findFirstIn mydata match {
    case Some(ticks(inside)) => println(inside)
    case _ => println("nothing")
}

for (ticks(inside) <- ticks findAllIn mydata) println(inside) // multiple matches

val Some(ticks(inside)) = ticks findFirstIn mydata // may throw exception

val ticks = ".*'([^']*)'.*".r    
val ticks(inside) = mydata // safe, shorter, only gets the first set of ticks

#9


1  

String da*t = mydata.split("'")[1];

字符串da*t = mydata.split(“”)[1];

See Live Demo

看到现场演示

#1


431  

Assuming you want the part between single quotes, use this regular expression with a Matcher:

假设您需要单引号之间的部分,请使用Matcher的正则表达式:

"'(.*?)'"

Example:

例子:

String mydata = "some string with 'the data i want' inside";
Pattern pattern = Pattern.compile("'(.*?)'");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find())
{
    System.out.println(matcher.group(1));
}

Result:

结果:

the data i want

#2


52  

You don't need regex for this.

你不需要regex。

Add apache commons lang to your project (http://commons.apache.org/proper/commons-lang/), then use:

将apache commons lang添加到项目中(http://commons.apache.org/proper/commons-lang/),然后使用:

String dataYouWant = StringUtils.substringBetween(mydata, "'");

#3


9  

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

public class Test {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile(".*'([^']*)'.*");
        String mydata = "some string with 'the data i want' inside";

        Matcher matcher = pattern.matcher(mydata);
        if(matcher.matches()) {
            System.out.println(matcher.group(1));
        }

    }
}

#4


8  

Because you also ticked Scala, a solution without regex which easily deals with multiple quoted strings:

因为您还修改了Scala,一个没有regex的解决方案,可以轻松处理多个引号字符串:

val text = "some string with 'the data i want' inside 'and even more data'"
text.split("'").zipWithIndex.filter(_._2 % 2 != 0).map(_._1)

res: Array[java.lang.String] = Array(the data i want, and even more data)

#5


3  

There's a simple one-liner for this:

这里有一句简单的俏皮话:

String target = myData.replaceAll("[^']*(?:'(.*?)')?.*", "$1");

By making the matching group optional, this also caters for quotes not being found by returning a blank in that case.

通过使匹配组可选,还可以通过返回空格来满足没有找到的引号。

See live demo.

看到现场演示。

#6


3  

String da*t = mydata.replaceFirst(".*'(.*?)'.*", "$1");

#7


2  

as in javascript:

在javascript中:

mydata.match(/'([^']+)'/)[1]

the actual regexp is: /'([^']+)'/

实际的regexp:/([^]+)/

if you use the non greedy modifier (as per another post) it's like this:

如果你使用非贪婪修饰符(根据另一篇文章),它是这样的:

mydata.match(/'(.*?)'/)[1]

it is cleaner.

它是清洁。

#8


2  

In Scala,

在Scala中,

val ticks = "'([^']*)'".r

ticks findFirstIn mydata match {
    case Some(ticks(inside)) => println(inside)
    case _ => println("nothing")
}

for (ticks(inside) <- ticks findAllIn mydata) println(inside) // multiple matches

val Some(ticks(inside)) = ticks findFirstIn mydata // may throw exception

val ticks = ".*'([^']*)'.*".r    
val ticks(inside) = mydata // safe, shorter, only gets the first set of ticks

#9


1  

String da*t = mydata.split("'")[1];

字符串da*t = mydata.split(“”)[1];

See Live Demo

看到现场演示