如何使用正则表达式提取子字符串

发布于 2024-10-11 05:02:31 字数 172 浏览 16 评论 0原文

我有一个字符串,其中有两个单引号,即 ' 字符。单引号之间是我想要的数据。

如何编写正则表达式从以下文本中提取“我想要的数据”?

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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

煮茶煮酒煮时光 2024-10-18 05:02:31

假设您想要单引号之间的部分,请将此正则表达式与 匹配器

"'(.*?)'"

示例:

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

结果:

the data i want

Assuming you want the part between single quotes, use this regular expression with a 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
绝不服输 2024-10-18 05:02:31

为此,您不需要正则表达式。

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

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

You don't need regex for this.

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

String dataYouWant = StringUtils.substringBetween(mydata, "'");
面犯桃花 2024-10-18 05:02:31
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));
        }

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

    }
}
花开雨落又逢春i 2024-10-18 05:02:31

为此有一个简单的单行:

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

通过将匹配组设置为可选,这也可以满足在这种情况下通过返回空白而找不到引号的情况。

请参阅现场演示

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.

暖伴 2024-10-18 05:02:31

从 Java 9

版本开始,您可以使用新方法 Matcher::results 没有参数,能够轻松返回 Stream其中 MatchResult 表示匹配操作的结果,并提供读取匹配组等内容(此类自 Java 1.5 起就已为人所知)。

String string = "Some string with 'the data I want' inside and 'another data I want'.";

Pattern pattern = Pattern.compile("'(.*?)'");
pattern.matcher(string)
       .results()                       // Stream<MatchResult>
       .map(mr -> mr.group(1))          // Stream<String> - the 1st group of each result
       .forEach(System.out::println);   // print them out (or process in other way...)

上面的代码片段的结果是:

我想要的数据
我想要的另一个数据

与过程式 if (matcher.find())while (matcher.find()) 相比,最大的优点是在有一个或多个结果可用时易于使用> 检查和处理。

Since Java 9

As of this version, you can use a new method Matcher::results with no args that is able to comfortably return Stream<MatchResult> where MatchResult represents the result of a match operation and offers to read matched groups and more (this class is known since Java 1.5).

String string = "Some string with 'the data I want' inside and 'another data I want'.";

Pattern pattern = Pattern.compile("'(.*?)'");
pattern.matcher(string)
       .results()                       // Stream<MatchResult>
       .map(mr -> mr.group(1))          // Stream<String> - the 1st group of each result
       .forEach(System.out::println);   // print them out (or process in other way...)

The code snippet above results in:

the data I want
another data I want

The biggest advantage is in the ease of usage when one or more results is available compared to the procedural if (matcher.find()) and while (matcher.find()) checks and processing.

皇甫轩 2024-10-18 05:02:31

因为您还勾选了 Scala,这是一个没有正则表达式的解决方案,可以轻松处理多个带引号的字符串:

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)

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

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)
╰つ倒转 2024-10-18 05:02:31
String dataIWant = mydata.replaceFirst(".*'(.*?)'.*", "$1");
String dataIWant = mydata.replaceFirst(".*'(.*?)'.*", "$1");
日记撕了你也走了 2024-10-18 05:02:31

就像在 javascript 中一样:

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

实际的正则表达式是: /'([^']+)'/

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

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

它更干净。

as in javascript:

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

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

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

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

it is cleaner.

我ぃ本無心為│何有愛 2024-10-18 05:02:31

String dataIWant = mydata.split("'")[1];

请参阅现场演示

String dataIWant = mydata.split("'")[1];

See Live Demo

饮湿 2024-10-18 05:02:31

Apache Commons Lang 为 java.lang API 提供了许多帮助实用程序,其中最著名的是字符串操作方法。
在您的情况下,开始和结束子字符串是相同的,因此只需调用以下函数即可。

StringUtils.substringBetween(字符串 str, 字符串标签)

获取嵌套在同一对象的两个实例之间的字符串
字符串

如果开始和结束子字符串不同,则使用以下重载方法。

StringUtils.substringBetween(字符串 str, 字符串打开, 字符串关闭)

获取嵌套在两个字符串之间的字符串。

如果您想要匹配子字符串的所有实例,请使用,

StringUtils.substringsBetween(字符串str,字符串打开,字符串关闭)

在字符串中搜索由开始和结束标记分隔的子字符串,
返回数组中所有匹配的子字符串

对于所讨论的示例,获取匹配子字符串的所有实例

String[] results = StringUtils.substringsBetween(mydata, "'", "'");

Apache Commons Lang provides a host of helper utilities for the java.lang API, most notably String manipulation methods.
In your case, the start and end substrings are the same, so just call the following function.

StringUtils.substringBetween(String str, String tag)

Gets the String that is nested in between two instances of the same
String
.

If the start and the end substrings are different then use the following overloaded method.

StringUtils.substringBetween(String str, String open, String close)

Gets the String that is nested in between two Strings.

If you want all instances of the matching substrings, then use,

StringUtils.substringsBetween(String str, String open, String close)

Searches a String for substrings delimited by a start and end tag,
returning all matching substrings in an array.

For the example in question to get all instances of the matching substring

String[] results = StringUtils.substringsBetween(mydata, "'", "'");
甜味超标? 2024-10-18 05:02:31

在斯卡拉中,

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

In 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
如日中天 2024-10-18 05:02:31

添加 apache.commons 对您的 pom.xml 的依赖

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>

,下面的代码可以工作。

StringUtils.substringBetween(String mydata, String "'", String "'")

add apache.commons dependency on your pom.xml

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>

And below code works.

StringUtils.substringBetween(String mydata, String "'", String "'")
薄情伤 2024-10-18 05:02:31

你可以用这个
我使用 while 循环将所有匹配子字符串存储在数组中

如果您使用if (matcher.find())
{
System.out.println(matcher.group(1));
}

您将获得匹配子字符串,因此您可以使用它来获取所有匹配子字符串

Matcher m = Pattern.compile("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+").matcher(text);
   // Matcher  mat = pattern.matcher(text);
    ArrayList<String>matchesEmail = new ArrayList<>();
        while (m.find()){
            String s = m.group();
            if(!matchesEmail.contains(s))
                matchesEmail.add(s);
        }

    Log.d(TAG, "emails: "+matchesEmail);

you can use this
i use while loop to store all matches substring in the array if you use

if (matcher.find())
{
System.out.println(matcher.group(1));
}

you will get on matches substring so you can use this to get all matches substring

Matcher m = Pattern.compile("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+").matcher(text);
   // Matcher  mat = pattern.matcher(text);
    ArrayList<String>matchesEmail = new ArrayList<>();
        while (m.find()){
            String s = m.group();
            if(!matchesEmail.contains(s))
                matchesEmail.add(s);
        }

    Log.d(TAG, "emails: "+matchesEmail);
娜些时光,永不杰束 2024-10-18 05:02:31

一些小组(1)对我不起作用。我使用 group(0) 来查找 url 版本。

Pattern urlVersionPattern = Pattern.compile("\\/v[0-9][a-z]{0,1}\\/");
Matcher m = urlVersionPattern.matcher(url);
if (m.find()) { 
    return StringUtils.substringBetween(m.group(0), "/", "/");
}
return "v0";

Some how the group(1) didnt work for me. I used group(0) to find the url version.

Pattern urlVersionPattern = Pattern.compile("\\/v[0-9][a-z]{0,1}\\/");
Matcher m = urlVersionPattern.matcher(url);
if (m.find()) { 
    return StringUtils.substringBetween(m.group(0), "/", "/");
}
return "v0";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文