Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Groovy regular expression to do a count of instances of X

  • 16-09-2013 11:04AM
    #1
    Registered Users, Registered Users 2 Posts: 5,763 ✭✭✭


    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,338 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