Title |
Test
Find
Pattern Title
|
Expression |
# Greedy, no-backtracking and only named groups capture.
(?n)
^
(
(?'allowDay'Sun|Mon|Fri)
|
(Sat
# if we match 'ur' then constrain
# the next char with a lookahead
# for 'd' so that we don't match on
# Satur.
(?'allowDay'ur(?=d))?
)
|
(
(
(
T
(
# Accepts "Tu.", "Tue."
ue?
|
# Accepts "Th.", "Thu." or "Thur."
h(ur?)?
)
)
|
(
Wed
# if "ne" matched then constrain
# the next chars with a lookahead
# for 'sd' so that we don't match on
# Wedne.
(ne(?=sd))?
)
)
(
# If 'Tue' or 'Thur' or 'Wedne' matched
# then match 's' in the "allowDay" Group.
(?<=(e|r))
(?'allowDay's)
)?
)
)
# Optional ending match of "day" or "."
(
# if Group[allowDay] succeeded then allow
# the letters "day" or a full-stop. If it
# did not succeed, a full-stop is required.
(?(allowDay)day)|\.
)?
$ |
Description |
|
Matches |
Th | Thu | Thurs |
Non-Matches |
Thursd | T | Thursdays |
Author |
Rating:
Darren Neimke
|
Source |
Michael Ash |
Your Rating |
|
Title: Should've mentioned...
Name: Darren Neimke
Date: 3/16/2004 4:08:50 AM
Comment:
Before I expanded the pattern it looked like this:
(?n)^((?'p1'(Sun|Mon|Fri))|(Sat(?'p1'ur(?=d))?)|(((T(ue?|h(ur?)?))|(Wed(ne(?=sd))?))((?<=(e|r))(?'p1's))?))((?(1)day)|\.)?$
Title: Altered to support PatternWhitespace
Name: Darren Neimke
Date: 3/16/2004 4:03:23 AM
Comment:
Updated this pattern to support PatternWhitespace. I'd love to get feedback from anybody drifting by as to whether this level of commenting is too verbose.
Title: To save myself some future work
Name: Darren Neimke
Date: 9/15/2003 11:59:55 PM
Comment:
...I'll expand the regex out:
(?n)
^
(
(?'p1'
(Sun|Mon|Fri)
)
|
(Sat
(?'p1'ur(?=d))?
)
|
(
(
(
T
(
ue?
|
h(ur?)?
)
)
|
(
Wed(ne(?=sd))?
)
)
(
(?<=(e|r))
(?'p1's)
)?
)
)
(
(?(1)day)|\.
)
?$
Title: Oooh... that's pretty sneaky
Name: Darren Neimke
Date: 9/15/2003 11:56:42 PM
Comment:
Took me a while to work out what you were doing on Wednesday :-)
Title: Re: Very True
Name: Michael Ash
Date: 9/15/2003 2:17:30 PM
Comment:
Sweet mod.
You had (Sun) in the first group twice and
it still lets a couple of bad values slip
through (Wednes and Satur)but more importantly I learned a
couple of new tricks with groupings from your version. I
tried rewriting it myself using some of the things I picked up from your's and finally came up with this
(?n)^((?'p1'(Sun|Mon|Fri))|(Sat(?'p1'ur(?=d))?)|(((T(ue?|h(ur?)?))|(Wed(ne(?=sd))?))((?<=(e|r))(?'p1's))?))((?(1)day)|\.)?$
Although a couple of the grouping seem to be specific to to .net RE's. specifically the named and look-behind groups
I tried it with a couple of other RE checkers and it wouldn't take the expression at all.
Again thanks for showing me something new.
Title: Very true
Name: Darren Neimke
Date: 9/12/2003 6:00:51 PM
Comment:
Thanks for the catch Michael! In a vain attempt to prove that mine *could* still work {grin} I'll alter my pattern to this:
(?ni)\b(?=[smtwf])(?'fb'Sun|Mon|Fri|Sun)|(Tu(e(?'fb's)?)?|Wed(?'fb'nes)?|Th(u(r(?'fb's)?)?)?|Sat(?'fb'ur)?)(?(1)(day|\.)?)\b
This uses conditional matching to check that the first, essential components are matched before matching on (day|\.).
Would I use mine or yours? Probably yours :-)
Title: Allow bad matches for days
Name: Michael Ash
Date: 9/12/2003 9:46:04 AM
Comment:
Will catch good days but also have some bad matches.
When I noticed how much shorter your RE was than mine
I wondered how you avoided some of the bad matches I struggled to avoid with trying to reduce the RE's size.
I try something similar to what you've done, but you've got the same problems I faced matching full names and abbreviations.
Bad matches : Wedday, Wednes, Thuday, Tuday