Details
-
Type:
Improvement
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Duplicate
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: groovy-jdk
-
Labels:None
-
Number of attachments :
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+)/)
)
{ // ... }Activity
Uri Moszkowicz
made changes -
| Field | Original Value | New Value |
|---|---|---|
| Status | Open [ 1 ] | Resolved [ 5 ] |
| Resolution | Duplicate [ 3 ] |
Paul King
made changes -
| Status | Resolved [ 5 ] | Closed [ 6 ] |
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.