Regular expressions have a bad reputation . It seems that whenever they are mentioned , It will call the image of the terrible text wall , These images look absolutely nonsense . for example , This is a common regular expression used to validate email addresses :
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/ =?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\ [\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0 -9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0- 5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0 -4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\ x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
Oh dear . I will not pretend that you will understand this expression at the end of this article , But at least I want to show you that it is based on simple rules , These rules are not difficult to understand .
You may want to know , Why should you care first about how these things work ? I think there are several good reasons . The first is that understanding the basics makes it easier to remember how to write good regular expressions .
I must have encountered many situations , I wrote a regular expression , Then you don't need to see it for months . When I finally got back to it , I've forgotten everything , Have to start from scratch