How to extract a substring using regex

前端 未结 14 1285
暖寄归人
暖寄归人 2020-11-22 13:37

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

相关标签:
14条回答
  • 2020-11-22 14:26

    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
    
    0 讨论(0)
  • 2020-11-22 14:28

    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)
    
    0 讨论(0)
  • 2020-11-22 14:28

    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.

    0 讨论(0)
  • 2020-11-22 14:29

    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, "'");
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 14:33
    String dataIWant = mydata.replaceFirst(".*'(.*?)'.*", "$1");
    
    0 讨论(0)
提交回复
热议问题