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

Regular Expressions in a Java if statement

  • 16-04-2015 7:32am
    #1
    Registered Users, Registered Users 2 Posts: 5,707 ✭✭✭


    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: 10,967 ✭✭✭✭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,707 ✭✭✭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: 10,967 ✭✭✭✭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 Posts: 2,031 ✭✭✭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,466 ✭✭✭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