What are good regular expressions?

后端 未结 9 921
我寻月下人不归
我寻月下人不归 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:18

    Regular Expressions (or Regex) are used to pattern match in strings. You can thus pull out all email addresses from a piece of text because it follows a specific pattern.

    In some cases regular expressions are enclosed in forward-slashes and after the second slash are placed options such as case-insensitivity. Here's a good one :)

    /(bb|[^b]{2})/i
    

    Spoken it can read "2 be or not 2 be".

    The first part are the (brackets), they are split by the pipe | character which equates to an or statement so (a|b) matches "a" or "b". The first half of the piped area matches "bb". The second half's name I don't know but it's the square brackets, they match anything that is not "b", that's why there is a roof symbol thingie (technical term) there. The squiggly brackets match a count of the things before them, in this case two characters that are not "b".

    After the second / is an "i" which makes it case insensitive. Use of the start and end slashes is environment specific, sometimes you do and sometimes you do not.

    Two links that I think you will find handy for this are

    1. regular-expressions.info
    2. Wikipedia - Regular expression

提交回复
热议问题