Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • October 01, 2016, 01:55:41 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: regular expressions 101  (Read 2596 times)

phitsc

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 1,186
    • View Profile
    • Donate to Member
regular expressions 101
« on: August 20, 2014, 02:44:22 PM »
While working with regular expressions today I stumbled over regular expressions 101. Not only does it test your regular expression as you type, but it also picks it apart and explains it to you piece by piece. Amazing!
« Last Edit: August 22, 2014, 10:53:11 AM by mouser »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 8,279
  • "In my dreams, I always do it right."
    • View Profile
    • Donate to Member
Re: regular expressions 101
« Reply #1 on: August 20, 2014, 02:48:21 PM »
There's also https://www.debuggex.com/?re=&str=

But personally, I use RegexBuddy.  It costs.  But I haven't found anything as robust.  And I'm willing to pay for my sanity when regularly confronted by regex like this...

Code: Text [Select]
  1. (?imnx)                                                                         #multiline mode, explicit captures only, with comments
  2. ^                                                                                       #start at beginning
  3. \s*                                                                                     #optional whitespace
  4. (                                           #start optional group
  5.         (                                       #begin OR
  6.                 (part|item|exhibit|note)            #match from a specific set of words
  7.                 |                                   # OR
  8.                 (?<unknown>\w+)                     #match any old word
  9.         )                                       #end OR
  10.         \s{1,2}                                 #limited whitespace between each one. capture certain specific ones.
  11. )?                                                                  #end optional group
  12. \(?                                         # optional open parenthesis
  13. (                                           # begin OR
  14.         ([\d]{1,2}\w?)                          # 1-2 numbers with an optional letter on the end
  15.         |                                       # OR
  16.         ([ivx]{1,3})                            # 1-2 roman numeral letters
  17.         |                                       # OR
  18.         ([a-z])                                 # 1 letters
  19. )                                           # end OR
  20. \)?                                         # optional end paren
  21. \s{0,2}                                                                         # optional whitespace
  22. (                                           # begin OR
  23.         ([\.](?(unknown)(\s{2,})|(\s+)))        # a period followed by at least one space if no unknown header, at least two spaces if unknown header
  24.         |                                       # OR
  25.         ([:]\s+)                                # colon followed by at least one space
  26.         |                                       # OR
  27.         (\p{Pd}+)                               # some number of dashes
  28.         |                                       # OR
  29.         (\)\s+)                                 # close paren followed by at leaset one space
  30. )                                                                       # end OR
  31. \s*                                         #optional whitespace

At least it's commented... right?

phitsc

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 1,186
    • View Profile
    • Donate to Member
Re: regular expressions 101
« Reply #2 on: August 20, 2014, 02:52:14 PM »
...
At least it's commented... right?

On the right, you mean :P

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 8,279
  • "In my dreams, I always do it right."
    • View Profile
    • Donate to Member
Re: regular expressions 101
« Reply #3 on: August 20, 2014, 02:57:26 PM »
...
At least it's commented... right?

On the right, you mean :P

amirite? :P

Seriously though, it's pretty impressive... though it doesn't seem to recognize global flags as in the beginning of that regex I posted.  It also doesn't seem to recognize line comments.  But that's a horrendous example... and what it does match, for a web app that costs nothing?  Impressive.
« Last Edit: August 20, 2014, 03:03:07 PM by wraith808 »

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 855
    • View Profile
    • Donate to Member
Re: regular expressions 101
« Reply #4 on: August 21, 2014, 04:30:26 PM »
Nice find! Let me regexpress my gratitude to phitsc for posting about it.