+ 2

Why would you ever match zero occurrences of an expression?

29th Jul 2016, 3:06 AM
jarrid beasley
jarrid beasley - avatar
2 Antworten
+ 2
Matching zero instances of an expression occurs most frequently when there is an optional part of the pattern. For a contrived example, the regex: at_least_a_thousand = r"[1-9]\d{0,2},(\d{3},)*\d{3}" ...will match "a digit between one and nine, followed by zero to two digits, followed by a comma, followed by zero or more instances of (exactly three digits followed by a comma), followed by exactly three digits." - an integer that is greater than or equal to 1,000, in other words. So it matches "1,234", "1,000,000", and "123,456", as well as "12,345,678,543,210", for instance. If there was some weird reason you needed to match a pretty-printed integer like that, you can use those two "at least zero" expressions so that you match everything from the minimal case on up with a single regex.
10th Oct 2016, 11:54 PM
Brian Gerard
0
Just came to my mind, that if you for example a checking an e-mail adress to be in right form, you want to be sure that there are no such letters as ",", "/", "'" that a prohibited. Correct me if I'm wrong.
5th Aug 2016, 6:12 AM
Kirill Pykhtin
Kirill Pykhtin - avatar