I’ve been looking through RegEx help for a bit now but I think I need a pro to help me through. I think this is possible with RegEx but can’t figure out how.
This is the match I am looking for. If it doesn’t match this exactly I want to know:
\w{2,4}\s\d{4}
I basically want to match for common school course names. The requirements are as follows:
- First part is 2-4 letters
- Second part is exactly 4 numbers
- There must be a space between the two
If there is any other variation to this I want to stop my next bit of code from running. I can easily trim the left and right end for spaces, but I want to make sure there is nothing else in there.
My problem is that my current expression allows someone to put in COMD 1500-B. I still get a match and I understand why, but I want to make sure if someone enters in more than 4 numbers I can stop and throw an error. As well as having more than 4 letters. I thought the {} was supposed to make a max.
^denotes start of string, and$denotes end of string. As such,^\w{2,4}\s\d{4}$ensures the entire string matches the pattern, rather than just a subset of the string matching.