What are good regular expressions?

后端 未结 9 934
我寻月下人不归
我寻月下人不归 2021-02-08 05:00

I have worked for 5 years mainly in java desktop applications accessing Oracle databases and I have never used regular expressions. Now I enter Stack Overflow and I see a lot of

相关标签:
9条回答
  • 2021-02-08 05:22

    If you want to learn about regular expressions, I recommend Mastering Regular Expressions. It goes all the way from the very basic concepts, all the way up to talking about how different engines work underneath. The last 4 chapters also gives a dedicated chapter to each of PHP, .Net, Perl, and Java. I learned a lot from it, and still use it as a reference.

    0 讨论(0)
  • 2021-02-08 05:23

    Validating strong passwords:

    This one will validate a password with a length of 5 to 10 alphanumerical characters, with at least one upper case, one lower case and one digit:

    ^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[a-zA-Z0-9]{5,10}$
    
    0 讨论(0)
  • 2021-02-08 05:26

    Coolest regular expression ever:

    /^1?$|^(11+?)\1+$/
    

    It tests if a number is prime. And it works!!

    N.B.: to make it work, a bit of set-up is needed; the number that we want to test has to be converted into a string of “1”s first, then we can apply the expression to test if the string does not contain a prime number of “1”s:

    def is_prime(n)
      str = "1" * n
      return str !~ /^1?$|^(11+?)\1+$/ 
    end
    

    There’s a detailled and very approachable explanation over at Avinash Meetoo’s blog.

    0 讨论(0)
提交回复
热议问题