
RegEx for validating YDS grades
Do you need to validate user inputs or filter climbs by grade? Here are two regular expressions to validate various formats of the Yosemite Decimal System. I decided to break them into two to make the regex more readable and easier to debug.
Whole numbers with + and -
# Support 5.0 to 5.15 with + and -
/^5\.([0-9]|1[0-5])([+-]?)$/
Valid: 5.0, 5.10+, 5.15
Not valid: Letter grade (a, b, c ,d) or anything less than 5.0
Letter grades
# Match 5.10x to 5.15x
/^5\.(1[0-5])([abcd])(?:\/[abcd])?$/
Valid: 5.10a, 5.10a/b
Invalid: anything less than 5.10, 5.10a+
Examples
Javascript
// Match whole numbers
const REGEX_5_X = RegExp(/^5\.([0-9]|1[0-5])([+-]?)$/);
// Match 5.10 and up with letter grades
const REGEX_5_10_LETTER = RegExp(/^5\.(1[0-5])([abcd])(?:\/[abcd])?$/);
let grade = "5.10c";
const m1 = REGEX_5_X.test(grade);
// output => false
const m2 = REGEX_5_10_LETTER.test(grade);
// output => true
Python (coming soon!)
