• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex for days of the week  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

This problem is confusing me as I don't know all that much about more advanced Regex's.

What I'm trying to do is create a pattern to match the days of the week, comma delimited. (i.e. mo,tu,we,th,su or we,fr,su).

The main problem I'm facing is the commas, even though the days should be in order the user can begin at any day of the week. Which leaves me with somewhat of a conditional statement such that whatever day begins the string should not include a comma, but the rest should.

Thanks in advanced for any help.

EDIT: here is the pattern I'm currently using.
(mo)?(,?tu)?(,?we)?(,?th)?(,?fr)?(,?sa)?(,?su)?

but the problem is this returns true for things like ,tu,fr and motuwe,th,fr
[ August 25, 2008: Message edited by: Charles Z Martin ]
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will help if you provide a list of what should match. And more importantly, what shouldn't match. Is this a list that should only contains days of the week? Or matches if it contains one day of the week? etc.

Henry
 
Charles Z Martin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the input can be any of the days of the week in the format of the first two letters in the name (i.e. 'mo' 'tu' 'we' 'th' 'fr' 'sa' or 'su'). These are case insensitive. The days of the week should be input in order starting from Monday.

The days of the week can be chosen at random as if they were being chosen from checkboxes and should be comma delimited. (i.e. 'mo,tu,we,th,fr,sa,su' would be everything you could possible choose. 'mo' or 'tu' or any other single day of the week is valid as well. And anything in between will work 'we,fr,sa,su' or 'mo,fr,su' the problem with my expression now is that by just saying the commas can be there or they cannot be there combinations such as 'motuweth' and ',we' and ',fr,sasu' work as well when they should not.

I guess I am looking for somewhat of a conditional statement. such that if it's the first combination in the input or the only combination in the input the comma should not be there, but if it has a preceding combination the comma should be there to separate the days.

Sorry for the long post.

EDIT:
Ok I am getting a bit closer with my new regex '(mo)?((^|,)tu)?((^|,)we)?((^|,)th)?((^|,)fr)?((^|,)sa)?((^|,)su)?'

Essentially what I'm looking for though is something along the lines of, If it is not the beginning of the input there must be a comma.

LAST EDIT:
Ok I got it working using the above regex and then just checking if the first character is a comma afterwards.
[ August 25, 2008: Message edited by: Charles Z Martin ]
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give this a try:
[ August 27, 2008: Message edited by: Piet Verdriet ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!