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.

Regular Expressions in a Java if statement

  • 16-04-2015 08:32AM
    #1
    Registered Users, Registered Users 2 Posts: 5,755 ✭✭✭


    See below current if statement code. Im not great with regexs. Can you advise how i would reformat this to make it shorter.

    if(!(securityDefinitionName.contains("Directory")) &&
    (!(securityDefinitionName.contains("OK"))) &&
    (!(securityDefinitionName.contains("/"))) &&
    (!(securityDefinitionName.contains("default"))))


Comments

  • Registered Users, Registered Users 2 Posts: 11,088 ✭✭✭✭28064212


    Why? What you have is a simple, efficient, readable implementation. Although you could probably get rid of some of the parentheses, just to make it slightly easier to read:
    if(!securityDefinitionName.contains("Directory") &&
    !securityDefinitionName.contains("OK") &&
    !securityDefinitionName.contains("/") &&
    !securityDefinitionName.contains("default"))
    
    Introducing regex would be highly unnecessary

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users, Registered Users 2 Posts: 5,755 ✭✭✭veryangryman


    28064212 wrote: »
    Why? What you have is a simple, efficient, readable implementation. Although you could probably get rid of some of the parentheses, just to make it slightly easier to read:
    if(!securityDefinitionName.contains("Directory") &&
    !securityDefinitionName.contains("OK") &&
    !securityDefinitionName.contains("/") &&
    !securityDefinitionName.contains("default"))
    
    Introducing regex would be highly unnecessary

    Thanks for reply. Am using Groovy, which (because of lack of semicolon dependance) doesn't like having multiple lines. I know theres way around that too, but am trying to get a good example of how to use regexs for this (and other stuff). StackOverflow doesnt seem to have a decent example.

    Looking for something along lines of

    if(!securityDefinitionName.contains("Directory|OK|/|default")

    to fit into a one liner without needing scrollbars :)


  • Registered Users, Registered Users 2 Posts: 11,088 ✭✭✭✭28064212


    You want the String.matches method. Off the top of my head
    if(!securityDefinitionName.matches(".*Directory.*|.*OK.*|.*/.*|.*default.*"))
    

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭lynchie


    Thanks for reply. Am using Groovy, which (because of lack of semicolon dependance) doesn't like having multiple lines. I know theres way around that too, but am trying to get a good example of how to use regexs for this (and other stuff). StackOverflow doesnt seem to have a decent example.

    Looking for something along lines of

    if(!securityDefinitionName.contains("Directory|OK|/|default")

    to fit into a one liner without needing scrollbars :)

    Firstly - There is absolutely nothing wrong with having multiple lines in groovy. The following is across multiple lines?
    def securityDefinitionName= "test test OK"
    
    if(!securityDefinitionName.contains("Directory") &&
    !securityDefinitionName.contains("OK") &&
    !securityDefinitionName.contains("/") &&
    !securityDefinitionName.contains("default"))
    println "Not found"
    else println "found"
    
    Secondly if you want to use a regex with groovy the following would work
    if(!(securityDefinitionName =~ /Directory|OK|default|\//))
    


  • Registered Users, Registered Users 2 Posts: 1,469 ✭✭✭Anesthetize


    See below current if statement code. Im not great with regexs. Can you advise how i would reformat this to make it shorter.

    if(!(securityDefinitionName.contains("Directory")) &&
    (!(securityDefinitionName.contains("OK"))) &&
    (!(securityDefinitionName.contains("/"))) &&
    (!(securityDefinitionName.contains("default"))))
    This implementation would be stricter and more readable than a regex. If you really want to reformat it, extract the code in the if condition to a method called securityDefinitionNameIsValid and pass in securityDefinitionName as an argument.

    Something like this:
    if (securityDefinitionNameIsValid(securityDefinitionName)) {
          ....
          ....
    }
    
    ....
    ....
    
    private boolean securityDefinitionNameIsValid(String securityDefinitionName) {
        return !securityDefinitionName.contains("Directory") && 
            !securityDefinitionName.contains("OK") && 
            !securityDefinitionName.contains("/") && 
            !securityDefinitionName.contains("default"));
    }
    
    Now the if statement looks far simpler and it's easier to understand what it's checking for :)


  • Advertisement
Advertisement