Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Groovy regular expression to do a count of instances of X

Options
  • 16-09-2013 12:04pm
    #1
    Registered Users Posts: 5,542 ✭✭✭


    Hi there, ive some code below to count number of instances of a String inside a larger Strin


    String uniqueNString = "Data=" + node.getName() + ",Attribute=1"

    int instancesOnThisNode = StringUtils.countMatches(sybaseQueryOutput, uniqueString)


    To make the code more flexible, I want to remove the necessity of importing Javas StringUtils class. Ive heard suggestions of doing same via regular expressions but i cant find anything specific online.

    Can anyone suggest?


Comments

  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    Have you looked at the Matcher?
    http://groovy.codehaus.org/groovy-jdk/java/util/regex/Matcher.html

    You'd make a reg expression of your uniqueNString

    Then apply that to your sybaseQueryOutput string creating a matcher.. something like
    def m = sybaseQueryOutput =~ uniqueNStringRegEx
    
    (the =~ is the important part there)

    then just look at m.count or evaluate m.size() to determine the number of matches found.


Advertisement