13

I have a Groovy method that currently works but is real ugly/hacky looking:

def parseId(String str) {
    System.out.println("str: " + str)
    int index = href.indexOf("repositoryId")
    System.out.println("index: " + index)
    int repoIndex = index + 13
    System.out.println("repoIndex" + repoIndex)
    String repoId = href.substring(repoIndex)
    System.out.println("repoId is: " + repoId)
}

When this runs, you might get output like:

str: wsodk3oke30d30kdl4kof94j93jr94f3kd03k043k?planKey=si23j383&repositoryId=31850514
index: 59
repoIndex: 72
repoId is: 31850514

As you can see, I'm simply interested in obtaining the repositoryId value (everything after the = operator) out of the String. Is there a more efficient/Groovier way of doing this or this the only way?

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

4 Answers4

21

There are a lot of ways to achieve what you want. I'll suggest a simple one using split:

sub = { it.split("repositoryId=")[1] }

str='wsodk3oke30d30kdl4kof94j93jr94f3kd03k043k?planKey=si23j383&repositoryId=31850514'

assert sub(str) == '31850514'
Will
  • 14,348
  • 1
  • 42
  • 44
  • I get an error: **no such property sub for class: org.gradle.api.internal.project.DefaultProject_Decorated** – IgorGanapolsky Aug 02 '16 at 22:06
  • 1
    @IgorGanapolsky are you running it from a script? try adding a `def` in front of `sub`. – Will Aug 03 '16 at 12:15
  • @Will, how do i get only the say next number and not the trailing ones after that? your solution is returning everything after the keyword. e.g. i have the String, "Request ID for the current retrieve task: 09Sg00000052eIJEAY [sf:retrieve] Waiting for server to finish processing the request...". I am trying to get only "09Sg00000052eIJEAY". I know i have to modify the index. But i can't figure so far – OK999 Jul 25 '17 at 15:52
  • Got it!!! def sub = { it.split("current retrieve task: ")[1][0..18] } .... Upvoted the answer :) – OK999 Jul 25 '17 at 15:59
16

Using a regular expression you could do

def repositoryId = (str =~ "repositoryId=(.*)")[0][1]

The =~ is a regex matcher

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
2

or a shortcut regexp - if you are looking only for single match:

String repoId = str.replaceFirst( /.*&repositoryId=(\w+).*/, '$1' )
injecteer
  • 20,038
  • 4
  • 45
  • 89
1

All the answers here contains regular expressions, however there are a bunch of string methods in Groovy.

String Function Sample Description
contains myStringVar.contains(substring) Returns true if and only if this string contains the specified sequence of char values
equals myStringVar.equals(substring) This is similar to the above but has to be an exact match for the check to return a true value
endsWith myStringVar.endsWith(suffix) This method checks the new value contains an ending string
startsWith myStringVar.startsWith(prefix) This method checks the new value contains an starting string
equalsIgnoreCase myStringVar.equalsIgnoreCase(substring) The same as equals but without case sensitivity
isEmpty myStringVar.isEmpty() Checks if myStringVar is populated or not.
matches myStringVar.matches(substring) This is the same as equals with the slight difference being that matches takes a regular string as a parameter unlike equals which takes another String object
replace myStringVar.replace(old,new) Returns a string resulting from replacing all occurrences of oldChar in this string with newChar
replaceAll myStringVar.replaceAll(old_regex,new) Replaces each substring of this string that matches the given regular expression with the given replacement
split myStringVar.split(regex) Splits this string around matches of the given regular expression
Foxy Fox
  • 403
  • 6
  • 13