code_junkie

How do you match only valid roman numerals with a regular expression?

Nov 12th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. ^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$
  2.  
  3. 0: <empty> matched by M{0}
  4. 1000: M matched by M{1}
  5. 2000: MM matched by M{2}
  6. 3000: MMM matched by M{3}
  7. 4000: MMMM matched by M{4}
  8.  
  9. 0: <empty> matched by D?C{0} (with D not there)
  10. 100: C matched by D?C{1} (with D not there)
  11. 200: CC matched by D?C{2} (with D not there)
  12. 300: CCC matched by D?C{3} (with D not there)
  13. 400: CD matched by CD
  14. 500: D matched by D?C{0} (with D there)
  15. 600: DC matched by D?C{1} (with D there)
  16. 700: DCC matched by D?C{2} (with D there)
  17. 800: DCCC matched by D?C{3} (with D there)
  18. 900: CM matched by CM
  19.  
  20. 0: <empty> matched by L?X{0} (with L not there)
  21. 10: X matched by L?X{1} (with L not there)
  22. 20: XX matched by L?X{2} (with L not there)
  23. 30: XXX matched by L?X{3} (with L not there)
  24. 40: XL matched by XL
  25. 50: L matched by L?X{0} (with L there)
  26. 60: LX matched by L?X{1} (with L there)
  27. 70: LXX matched by L?X{2} (with L there)
  28. 80: LXXX matched by L?X{3} (with L there)
  29. 90: XC matched by XC
  30.  
  31. 0: <empty> matched by V?I{0} (with V not there)
  32. 1: I matched by V?I{1} (with V not there)
  33. 2: II matched by V?I{2} (with V not there)
  34. 3: III matched by V?I{3} (with V not there)
  35. 4: IV matched by IV
  36. 5: V matched by V?I{0} (with V there)
  37. 6: VI matched by V?I{1} (with V there)
  38. 7: VII matched by V?I{2} (with V there)
  39. 8: VIII matched by V?I{3} (with V there)
  40. 9: IX matched by IX
  41.  
  42. <opt-thousands-part><opt-hundreds-part><opt-tens-part><opt-units-part>
  43.  
  44. <opt-hundreds-part> = m/(CM|DC{0,3}|CD|C{1,3})?/;
  45.  
  46. <opt-hundreds-part> = m/(C[MD]|D?C{0,3})/;
  47.  
  48. <opt-hundreds-part> = m/(?:C[MD]|D?C{0,3})/;
  49.  
  50. <opt-hundreds-part> = m/(?:[IXC][MD]|D?C{0,4})/;
  51.  
  52. ^(M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})|[IDCXMLV])$
Add Comment
Please, Sign In to add comment