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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Groovy regular expression to do a count of instances of X

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


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