tags:

views:

32

answers:

3

I'm a bit rubbish with Regex and was wondering if some kind person would be happy to assist!

The format is:

optionally 1, 2 , A or B

Day Name, possibly short or full, eg Mon, Tuesday, Thursday, Fr

Colon : or Space

Integer

The following are valid: EDIT: included "Mon:20"

Mon:3, Tuesday:6, AWe:9, 2Fr:2, 2Wed 3, Friday 6, Mon:20

The following is not valid:

3Mon:1, Mon3, 3:Wed

I would presume for the day name, we can just check for A-Z,a-z starting with Mo,Tu,We,Th,Fr,Sa,Su ending with a colon or use piping and specify every possibility ie Mo|Mon|Monday|Tu|Tue|Tues|Tuesday, etc.

Thanks ever so much

+1  A: 
[12AB]?(Mo|Tu|We|Th|Fr|Sa|Su)\w*[: ]\d+
eumiro
You don't need to escape colon - neither inside or outside of a character class.
Peter Boughton
@Peter - colon unescaped, thanks
eumiro
Here Mon:3 isn't valid, but AMontagada:3 is.
Colin Hebert
Thanks ever so much. Think I just needed started, can play about with this with a RegEx reference. Really appreciated!
Igor K
+1  A: 

This checks the days of the week more strictly than the other answer:

/[12AB]?(
    Mon?|
    Tu(es?)?|
    We(d(n(es?)?)?)?|
    Th(u(rs?)?)?|
    Fri?|
    Sa(t(ur?)?)?|
    Sun?
)(d(ay?)?)?
[: ]\d+/
Ether
Thanks ever so much. Think I just needed started, can play about with this with a RegEx reference. Really appreciated!
Igor K
+1, It's kind of ugly but yet the best way using regexes. (You could aslo simplify the day part for every day of the week).
Colin Hebert
@Colin: ooh yeah; done! :)
Ether
Just tried this with several online checkers for AMon:1 and Mon 1, returned False. Any ideas?
Igor K
Ps although that looks great, Mondat would be considered a match when it's not. Whats the best way of sorting this?
Igor K
@Igor: are you sure? I don't see how it would match given the latest version of the pattern above - the `(d(ay?)?)?` portion is immediately followed by either a colon or a space.
Ether
@Ether apologies, it did work. In the tester I was using, had to remove the leading "/" and last "/" characters.
Igor K
@Igor: a ha! yes, some languages use `//` for regexes, and some do not.
Ether
A: 

Realised I needed to return true for

TuA:2

This is the solution I've ended up with for the moment:

([12AB]?)[: ]*(Mo|Tu|We|Th|Fr|Sa|Su)(\w*)[: ]*(\d)+

This allows me to pick out the leading A,B,1,2, the day Mo,Tu,We... and whats after the day, "A" in the example above or otherwise "esday" for "Tuesday", and it returns the last digit too.

Thanks everyone for putting me on the right track.

Igor K