News for hackers. Stuff that matters.
Re:Visual Support for Working with Regular Expressions by chinpokomon on Tuesday January 13, 2015 @10:47AM (#8880517)
I couldn't agree more. In .NET I use something like this:

new Regex(@"

                                # {begining of string}International Call Prefix, 

                                # digits [0 - 2]
                        ^[+]1

                                # Area Code validation allowing only US landline and mobile numbers, 

                                # digits [3 - 5]

                        (?:

                             2(?:0[1-35-9]|1[02-9]|2[4589]|3[149]|4[08]|5[1-46]|6[0279]|7[026]|8[13])

                            |3(?:0[1-57-9]|1[02-9]|2[0135]|3[014679]|4[67]|5[12]|6[014]|8[56])

                            |4(?:0[124-9]|1[02-579]|2[3-5]|3[0245]|4[0235]|58|69|7[0589]|8[04])

                            |5(?:0[1-57-9]|1[0235-8]|20|3[0149]|4[01]|5[19]|6[1-37]|7[013-5]|8[056])

                            |6(?:0[1-35-9]|1[024-9]|2[036]|3[016]|4[16]|5[017]|6[0-279]|78|8[12])

                            |7(?:0[1-46-8]|1[02-9]|2[0457]|3[1247]|4[07]|5[47]|6[02359]|7[02-59]|8[156])

                            |8(?:0[1-68]|1[02-8]|28|3[0-25]|4[3578]|5[06-9]|6[02-5]|7[028])

                            |9(?:0[1346-9]|1[02-9]|2[0589]|3[01678]|4[0179]|5[12469]|7[0-3589]|8[0459])

                        )

                                #

                                # digit [6]

                        [2-9]

                                # Remaining 6 digits can be any digit{end of string}

                                # digits [7 - 12]
                        \d{6}$",

                        RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);
The key part is "RegexOptions.IgnorePatternWhitespace". With that, you can add comments to your Regex using the "#" character and break it up even more. In this example I didn't use capture groups, but I usually use a combination of both techniques.

Edit: Formatting of whitespace was somewhat lost with the post. In my code it looks great! I also cleaned up some of the comments that revealed more information than I intended.

There are no comments on this comment yet.