Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: groovy-jdk
    • Labels:
      None
    • Number of attachments :
      0

      Description

      Collection implements a method with the following signature:

      public Object findResult(Closure closure)

      It would be nice if a similar method existed for String.

      public Object findResult(Pattern pattern, Closure closure)

      The method find() almost performs the same function except that it converts the result to a string, even if it's null or a Boolean. Alternatively, the existing find() methods could be changed to return Object instead of String. Without this method, the matching string has to be saved into a temporary variable and then processed afterwards.

      Example:
      if (new File("file").text.findResult(~/VAR\s*=\s*(\S+)/)

      { it[1] != "VALUE1" }

      )

      { // ... }

        Activity

        Hide
        Uri Moszkowicz added a comment -

        I omitted an important aspect of the example:

        if (new File("file").text.findResult(~/VAR\s*=\s*(\S+)/) { (it[1[] =~ /VALUE0/) && (it[1] != "VALUE1"})) {
          // ...
        }
        

        A variable would be needed otherwise because it is used multiple times in the closure.

        Show
        Uri Moszkowicz added a comment - I omitted an important aspect of the example: if (new File("file").text.findResult(~/VAR\s*=\s*(\S+)/) { (it[1[] =~ /VALUE0/) && (it[1] != "VALUE1"})) { // ... } A variable would be needed otherwise because it is used multiple times in the closure.
        Hide
        Paul King added a comment - - edited

        I guess it is a little clearer than below but not by a lot:

        if (new File("file").text.find(~/VAR\s*=\s*(\S+)/) { (it[1] =~ /VALUE0/ && it[1] != "VALUE1") ?: '' }) {
          // ...
        }
        
        Show
        Paul King added a comment - - edited I guess it is a little clearer than below but not by a lot: if ( new File( "file" ).text.find(~/VAR\s*=\s*(\S+)/) { (it[1] =~ /VALUE0/ && it[1] != "VALUE1" ) ?: '' }) { // ... }
        Hide
        Pascal Schumacher added a comment -

        Is this a duplicate of GROOVY-5784 or am I missing something?

        Show
        Pascal Schumacher added a comment - Is this a duplicate of GROOVY-5784 or am I missing something?

          People

          • Assignee:
            Unassigned
            Reporter:
            Uri Moszkowicz
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: