DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: regular expressions 101  (Read 9,958 times)

phitsc

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 1,198
    • Participated in NANY Challenge Seven Times
    • Plugin Coding Medal
regular expressions 101
« on: August 20, 2014, 07:44 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, 03:53 PM by mouser »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: regular expressions 101
« Reply #1 on: August 20, 2014, 07:48 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: (generic)
(?imnx)										#multiline mode, explicit captures only, with comments
^											#start at beginning
\s*											#optional whitespace
(                                           #start optional group
	(                                       #begin OR
		(part|item|exhibit|note)            #match from a specific set of words
		|                                   # OR
		(?<unknown>\w+)                     #match any old word
	)                                       #end OR
	\s{1,2}                                 #limited whitespace between each one. capture certain specific ones.
)?  							            #end optional group
\(?                                         # optional open parenthesis
(                                           # begin OR
	([\d]{1,2}\w?)                          # 1-2 numbers with an optional letter on the end
	|                                       # OR
	([ivx]{1,3})                            # 1-2 roman numeral letters
	|                                       # OR
	([a-z])                                 # 1 letters
)                                           # end OR
\)?                                         # optional end paren
\s{0,2}										# optional whitespace
(                                           # begin OR
	([\.](?(unknown)(\s{2,})|(\s+)))        # a period followed by at least one space if no unknown header, at least two spaces if unknown header
	|                                       # OR
	([:]\s+)                                # colon followed by at least one space
	|                                       # OR
	(\p{Pd}+)                               # some number of dashes
	|                                       # OR
	(\)\s+)                                 # close paren followed by at leaset one space
)							                # end OR
\s*                                         #optional whitespace

At least it's commented... right?

phitsc

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 1,198
    • Participated in NANY Challenge Seven Times
    • Plugin Coding Medal
Re: regular expressions 101
« Reply #2 on: August 20, 2014, 07:52 PM »
...
At least it's commented... right?

On the right, you mean :P

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: regular expressions 101
« Reply #3 on: August 20, 2014, 07:57 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, 08:03 PM by wraith808 »

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,175
    • Coding Snacks
    • Participated in NANY Challenge Three Times
    • Plugin Coding Medal
Re: regular expressions 101
« Reply #4 on: August 21, 2014, 09:30 PM »
Nice find! Let me regexpress my gratitude to phitsc for posting about it.

Original address: https://www.donationcoder.com/forum/index.php?topic=38755.0