Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

66%
+2 −0
Challenges The 50 substrings that validate any string of Roman numerals

Perl, 119 bytes Read a single Roman numeral on stdin (optionally newline-terminated). Outputs nothing unless one of the 50 strings is found, in which case it is printed (without trailing newline):...

posted 7mo ago by jhnc‭  ·  edited 6mo ago by jhnc‭

Answer
#5: Post edited by user avatar jhnc‭ · 2026-03-09T17:01:43Z (6 months ago)
optimise regex
  • Perl, 125 bytes
  • ---
  • Read a single Roman numeral on stdin (optionally newline-terminated). Outputs nothing unless one of the 50 strings is found, in which case it is printed (without trailing newline):
  • ```
  • perl -pe'($_)=/CCCC|C[CM][DM]|C[DM]C|IIII|I[IX][VX]|I[VX]I|MMMM|VI?[VX]|XC[CLX]|XLX|XXXX|[DX]C?[DM]|[ILV][CDLM]|[ILX]X[CL]/g'
  • ```
  • Minimising a regex is nontrivial but will probably be the bulk of any solution. I expect it is possible to shrink it even more than this attempt.
  • * `-p` - load each line of input as `$_`, manipulate, then print
  • * `-e` - program follows
  • * `/.../g` - return list of all matches
  • * `($_) = ...` - store first match in `$_` (if no matches, assigns empty string)
  • To read a newline-separated list of numerals and to print "VALID" instead of nothing increases code to 132 bytes:
  • ```
  • perl -ple'$_=/CCCC|C[CM][DM]|C[DM]C|IIII|I[IX][VX]|I[VX]I|MMMM|VI?[VX]|XC[CLX]|XLX|XXXX|[DX]C?[DM]|[ILV][CDLM]|[ILX]X[CL]/?$&:VALID'
  • ```
  • * `$_ = /.../ ? $& : VALID` - ternary assignment to `$_` - if a match is found, assign its value (`$&`), otherwise assign bareword `VALID`
  • [Try it online!][TIO-mmdmcwbq]
  • [TIO-mmdmcwbq]: https://tio.run/##LVLBitswFLzrKx67obm02VMvW0oo0kWgdxUPjNl4HW3iJrGMpTQU/O1NR8oKniXNmxmNhKcwn7/fn8nSPo7rTKcx3uiIypEOIVM@Dqmsu74PU6bLX@rjPtB17I/deAh79Uwpkl3/CRTGHOawhyTQLqxXbz83m816R1M3Z@oS/b6mTLsHvKNhrMTqlkKfhzjSezjHm4LlR5wph5SH8fD1EeE8jKHAly6nKozXPF1ziXaO8UQdvQ8ZrFOo3SKmvkshwa2PlwvCEQhQVfO5u9Ec0vWck5rmYcwf9LR6o1d6@nFHwheNsehGc9sYbrHCVy8WY7GNlbbx0mKFr10YY/F2WzGByJXZySIYS2Ok1dvq0ljn20Yb91hLK@C2L9vVl1f/y1lzv1vllSintDKKlcUWewBAAAErIFCU9Sjr0bCoMguqEDXKoCrXKnYGHxEYaGtFsS65MGEITJid6OKIa2gWXSjMBl0Hnq8WhSzOq0qoGjGfDQPFJ4UN@KqYa@dEYWeYSzJjjGXE906L9cLai0Mc7yxeBCHEsyt3xcvgWjjWQF0ic@mzc8UFR0IPL4e31QoBRJcOQK53Bd9gVzQ4yxiuEHIZzfwvTuUXS/dv0/k/ "Perl 5 – Try It Online"
  • Perl, 119 bytes
  • ---
  • Read a single Roman numeral on stdin (optionally newline-terminated). Outputs nothing unless one of the 50 strings is found, in which case it is printed (without trailing newline):
  • ```
  • perl -pe'($_)=/([CIMX])\1{3}|C[CM][DM]|C[DM]C|I[IX][VX]|I[VX]I|VI?[VX]|XC[CLX]|XLX|[DX]C?[DM]|[ILV][CDLM]|[ILX]X[CL]/g'
  • ```
  • Minimising a regex is nontrivial but will probably be the bulk of any solution. I expect it is possible to shrink it even more than this attempt.
  • * `-p` - load each line of input as `$_`, manipulate, then print
  • * `-e` - program follows
  • * `/.../g` - return list of all matches
  • * `($_) = ...` - store first match in `$_` (if no matches, assigns empty string)
  • To read a newline-separated list of numerals and to print "VALID" instead of nothing increases code to 126 bytes:
  • ```
  • perl -ple'$_=/([CIMX])\1{3}|C[CM][DM]|C[DM]C|I[IX][VX]|I[VX]I|VI?[VX]|XC[CLX]|XLX|[DX]C?[DM]|[ILV][CDLM]|[ILX]X[CL]/?$&:VALID'
  • ```
  • * `$_ = /.../ ? $& : VALID` - ternary assignment to `$_` - if a match is found, assign its value (`$&`), otherwise assign bareword `VALID`
  • [Try it online!][TIO-mmjfcy6i]
  • [TIO-mmjfcy6i]: https://tio.run/##XVTRahsxEHy/r1iSULfFsSmlLwltKNKLQHoVC9cjvpyVxI19Z3xyQ6n763Vmdc5F1GDdnDwzmtUu3obd@svxnAwtu3YS6antnukR39jRQ4gUH1e94LppwjbS5jc13TLQvm0e6/YhLItz6jsyk1@BQhvDLiwhCbQIk4vbr7PZbLKgbb2LVPf0c99HWgzbC1q1iZjc@tDEVdfSXVh3zwUs77sdxdDHVfswHSKsV22Q7U0d@yTs9nG7jxJt3XVPVNPdKoL1FNKvIqam7kMPt6bbbBCOQIAqme/qZ9qFfr@OfbHdrdp4T2cXt3T5jc6ui35OV7OP8/n1EWHn70tlHFcffnz68/nvQZXKVaV2FRBWdTCl4ar0XAFhNQdvbtIrg2rlaflQaq7UTZKVxvqqVNoOmCsGrZrfXLy78t@t0cejoStKsPAj4hHZEakR6RG5EZnMJvn4gjMXsbG2UJmJuGhY5B6ZSYaNF4wVDPHGCqe335XyGeYMZ@mzo5WSs7ECuYScnJ0OwadwVgu2CMcsMqyFVsZwYnDhFHOqTh54w4dTRkGoyLIa8gPg1SnHyrzlwo4G0ULs85qdOLF9KyYJfU5gPQg4sTVs/9M7DdOhpinhreCsDcpaHhoBlnZOiBo8TneitXGnrkzJiBw35K1i49kpzzbdIE/J2ylZBZkrtLcGE4by2TtrMoLB18PEKwwExg/tQ8E6hfHY1gOF8bSgSXe1XLcTO2ftGE5YVszUwAQHC1qGy8HxEtxi6NUp@BgOGNtT6UEqRZAtcIGsxB1Kl7qf0mQiOSslSjqXDrOYD5yroZJ84y29al@rGfOBPZVREbHMukt69EerNPAi1u7kj61cplPr9FDlKbpM3b9uK39b/fFyu34B "Perl 5 – Try It Online"
#4: Post edited by user avatar jhnc‭ · 2026-03-05T15:28:58Z (7 months ago)
smaller regex; only accept single numeral
  • Perl, 135 bytes
  • ---
  • Takes newline-separated list of Roman numerals as input. Outputs nothing unless one of the 50 strings is found, in which case it is printed:
  • ```
  • perl -ple'($_)=/CCCC|IIII|MMMM|XXXX|CDC|CM[CDM]|I[IX][VX]|I[VX]I|VI[VX]|V[VX]|XC[CLX]|XLX|[CDX]C[DM]|[DX][DM]|[ILV][CDLM]|[ILX]X[CL]/g'
  • ```
  • Minimising a regex is nontrivial but seems to be the bulk of any code. I expect it is possible to shrink it even more than this attempt.
  • * `-l` - strip newline from input and add it back on output
  • * `-p` - load each line of input as `$_`, manipulate, then print
  • * `-e` - program follows
  • * `/.../g` - return list of all matches
  • * `($_) = ...` - store first match in `$_` (if no matches, assigns empty string)
  • To print "VALID" instead of nothing increases code to 141 bytes:
  • ```
  • perl -ple'$_=/CCCC|IIII|MMMM|XXXX|CDC|CM[CDM]|I[IX][VX]|I[VX]I|VI[VX]|V[VX]|XC[CLX]|XLX|[CDX]C[DM]|[DX][DM]|[ILV][CDLM]|[ILX]X[CL]/?$&:VALID'
  • ```
  • * `$_ = /.../ ? $& : VALID` - ternary assignment to `$_` - if a match is found, assign its value (`$&`), otherwise assign bareword `VALID`
  • [Try it online!][TIO-mmdh20oa]
  • [TIO-mmdh20oa]: https://tio.run/##LVLBitswEL3rK4bd0Fza7KmXLWUp0kUgXcWAMRuvo03cOJaxlC4Ff3vTN8oOjDWaee/pSXiOy/j99kiWDmnaFjpP6YNOyJLoGAuV05Cl7vo@zoUuf6lPh0jXqT910zEe1CPlRHb7J1KcSlziAZRI@7jdvP7c7XbbPc3dUqjL9PuaC@3v7T0NUwVWtRz7MqSJ3uKYPhQk39NCJeYyTMevdwvjMEVpX7qSKzFdy3wtYm1M6UwdvQ0FqHOsUyFT3@WYodanywXmCACwqvjSfdAS83UsWc3LMJV3eti80jM9/LjB4ZNGrBaxesTKiFUbvWrfaOPb1TaW2yawVPjaNdR1DfXLutFOVscr8NzqRkgNqnthXWgxcPeaWwa@fXrZfHkOv5w1t5tVQbFySiujvLLYYo8GOmihJ010kTYgbcDAImVlpAA10iAr1irvDD7MENDWsvJaroUFwRDx3rEWRdxYe9YC8d5g6oALVULA7IKqgMph8zkwYHxCvAFeibh2jhV2xntxZoyxHvaD02wDex3YwU5wFi8CExy8k7viZXAtHGvAFste5t45UcGR4EPL4X21ggHWMkHT17sCb7ATDs4yxtcWfBnt/b80y7@Wb9/m8T8 "Perl 5 – Try It Online"
  • Perl, 125 bytes
  • ---
  • Read a single Roman numeral on stdin (optionally newline-terminated). Outputs nothing unless one of the 50 strings is found, in which case it is printed (without trailing newline):
  • ```
  • perl -pe'($_)=/CCCC|C[CM][DM]|C[DM]C|IIII|I[IX][VX]|I[VX]I|MMMM|VI?[VX]|XC[CLX]|XLX|XXXX|[DX]C?[DM]|[ILV][CDLM]|[ILX]X[CL]/g'
  • ```
  • Minimising a regex is nontrivial but will probably be the bulk of any solution. I expect it is possible to shrink it even more than this attempt.
  • * `-p` - load each line of input as `$_`, manipulate, then print
  • * `-e` - program follows
  • * `/.../g` - return list of all matches
  • * `($_) = ...` - store first match in `$_` (if no matches, assigns empty string)
  • To read a newline-separated list of numerals and to print "VALID" instead of nothing increases code to 132 bytes:
  • ```
  • perl -ple'$_=/CCCC|C[CM][DM]|C[DM]C|IIII|I[IX][VX]|I[VX]I|MMMM|VI?[VX]|XC[CLX]|XLX|XXXX|[DX]C?[DM]|[ILV][CDLM]|[ILX]X[CL]/?$&:VALID'
  • ```
  • * `$_ = /.../ ? $& : VALID` - ternary assignment to `$_` - if a match is found, assign its value (`$&`), otherwise assign bareword `VALID`
  • [Try it online!][TIO-mmdmcwbq]
  • [TIO-mmdmcwbq]: https://tio.run/##LVLBitswFLzrKx67obm02VMvW0oo0kWgdxUPjNl4HW3iJrGMpTQU/O1NR8oKniXNmxmNhKcwn7/fn8nSPo7rTKcx3uiIypEOIVM@Dqmsu74PU6bLX@rjPtB17I/deAh79Uwpkl3/CRTGHOawhyTQLqxXbz83m816R1M3Z@oS/b6mTLsHvKNhrMTqlkKfhzjSezjHm4LlR5wph5SH8fD1EeE8jKHAly6nKozXPF1ziXaO8UQdvQ8ZrFOo3SKmvkshwa2PlwvCEQhQVfO5u9Ec0vWck5rmYcwf9LR6o1d6@nFHwheNsehGc9sYbrHCVy8WY7GNlbbx0mKFr10YY/F2WzGByJXZySIYS2Ok1dvq0ljn20Yb91hLK@C2L9vVl1f/y1lzv1vllSintDKKlcUWewBAAAErIFCU9Sjr0bCoMguqEDXKoCrXKnYGHxEYaGtFsS65MGEITJid6OKIa2gWXSjMBl0Hnq8WhSzOq0qoGjGfDQPFJ4UN@KqYa@dEYWeYSzJjjGXE906L9cLai0Mc7yxeBCHEsyt3xcvgWjjWQF0ic@mzc8UFR0IPL4e31QoBRJcOQK53Bd9gVzQ4yxiuEHIZzfwvTuUXS/dv0/k/ "Perl 5 – Try It Online"
#3: Post edited by user avatar jhnc‭ · 2026-03-05T13:09:42Z (7 months ago)
link to tio
  • Perl, 135 bytes
  • ---
  • Takes newline-separated list of Roman numerals as input. Outputs nothing unless one of the 50 strings is found, in which case it is printed:
  • ```
  • perl -ple'($_)=/CCCC|IIII|MMMM|XXXX|CDC|CM[CDM]|I[IX][VX]|I[VX]I|VI[VX]|V[VX]|XC[CLX]|XLX|[CDX]C[DM]|[DX][DM]|[ILV][CDLM]|[ILX]X[CL]/g'
  • ```
  • Minimising a regex is nontrivial but seems to be the bulk of any code. I expect it is possible to shrink it even more than this attempt.
  • * `-l` - strip newline from input and add it back on output
  • * `-p` - load each line of input as `$_`, manipulate, then print
  • * `-e` - program follows
  • * `/.../g` - return list of all matches
  • * `($_) = ...` - store first match in `$_` (if no matches, assigns empty string)
  • To print "VALID" instead of nothing increases code to 141 bytes:
  • ```
  • perl -ple'$_=/CCCC|IIII|MMMM|XXXX|CDC|CM[CDM]|I[IX][VX]|I[VX]I|VI[VX]|V[VX]|XC[CLX]|XLX|[CDX]C[DM]|[DX][DM]|[ILV][CDLM]|[ILX]X[CL]/?$&:VALID'
  • ```
  • * `$_ = /.../ ? $& : VALID` - ternary assignment to `$_` - if a match is found, assign its value (`$&`), otherwise assign bareword `VALID`
  • [Try it online!][TIO-mmdh20oa]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mmdh20oa]: https://tio.run/##LVBNi9tADL3Prxjo0lza7KmXQilFugik6yAwpkmTYbNtapvY26Uwv73uk7MCjaSnp6@Z6u36aX2XJZ/HYbfkX8P4mi/QZcxPdcnL5XkO/3g61WnJv//m03iu@WU4XY7DUz1/yPOYZfen5jos9VbPqKj5UHcP37/s9/vdIU/H25KPc/75Mi/5cIcP@Ue9jq8rokeCNIE0gzSHNGJqZB2x9U068b4rHh5eaWWzrWyvU0caVr2B7z11UdTBuzuipUdC7773Dn7/@PXh/efyTYXXVVJJnjRR4mRJECIGAAQQsACBQqVApSAh0LAODSJBGbpxJZkyHnc0IBFPRnEWDMTRxEydoiMuJnMKihkjq@CVrUWQXUvaCFuN81uCUfFGMQY/RXNS9YSIzWIzZhbD@kXJpbhRccU6RQU/giW8mMat@BmchbGM6ljZIm@q0QUjUY9eiv@lhAWcIgPQtlvBZ0RRg1nMtkHYi8ns3zgtz@Mwrx@n638 "Perl 5 – Try It Online"
  • Perl, 135 bytes
  • ---
  • Takes newline-separated list of Roman numerals as input. Outputs nothing unless one of the 50 strings is found, in which case it is printed:
  • ```
  • perl -ple'($_)=/CCCC|IIII|MMMM|XXXX|CDC|CM[CDM]|I[IX][VX]|I[VX]I|VI[VX]|V[VX]|XC[CLX]|XLX|[CDX]C[DM]|[DX][DM]|[ILV][CDLM]|[ILX]X[CL]/g'
  • ```
  • Minimising a regex is nontrivial but seems to be the bulk of any code. I expect it is possible to shrink it even more than this attempt.
  • * `-l` - strip newline from input and add it back on output
  • * `-p` - load each line of input as `$_`, manipulate, then print
  • * `-e` - program follows
  • * `/.../g` - return list of all matches
  • * `($_) = ...` - store first match in `$_` (if no matches, assigns empty string)
  • To print "VALID" instead of nothing increases code to 141 bytes:
  • ```
  • perl -ple'$_=/CCCC|IIII|MMMM|XXXX|CDC|CM[CDM]|I[IX][VX]|I[VX]I|VI[VX]|V[VX]|XC[CLX]|XLX|[CDX]C[DM]|[DX][DM]|[ILV][CDLM]|[ILX]X[CL]/?$&:VALID'
  • ```
  • * `$_ = /.../ ? $& : VALID` - ternary assignment to `$_` - if a match is found, assign its value (`$&`), otherwise assign bareword `VALID`
  • [Try it online!][TIO-mmdh20oa]
  • [TIO-mmdh20oa]: https://tio.run/##LVLBitswEL3rK4bd0Fza7KmXLWUp0kUgXcWAMRuvo03cOJaxlC4Ff3vTN8oOjDWaee/pSXiOy/j99kiWDmnaFjpP6YNOyJLoGAuV05Cl7vo@zoUuf6lPh0jXqT910zEe1CPlRHb7J1KcSlziAZRI@7jdvP7c7XbbPc3dUqjL9PuaC@3v7T0NUwVWtRz7MqSJ3uKYPhQk39NCJeYyTMevdwvjMEVpX7qSKzFdy3wtYm1M6UwdvQ0FqHOsUyFT3@WYodanywXmCACwqvjSfdAS83UsWc3LMJV3eti80jM9/LjB4ZNGrBaxesTKiFUbvWrfaOPb1TaW2yawVPjaNdR1DfXLutFOVscr8NzqRkgNqnthXWgxcPeaWwa@fXrZfHkOv5w1t5tVQbFySiujvLLYYo8GOmihJ010kTYgbcDAImVlpAA10iAr1irvDD7MENDWsvJaroUFwRDx3rEWRdxYe9YC8d5g6oALVULA7IKqgMph8zkwYHxCvAFeibh2jhV2xntxZoyxHvaD02wDex3YwU5wFi8CExy8k7viZXAtHGvAFste5t45UcGR4EPL4X21ggHWMkHT17sCb7ATDs4yxtcWfBnt/b80y7@Wb9/m8T8 "Perl 5 – Try It Online"
#2: Post edited by user avatar jhnc‭ · 2026-03-05T13:00:52Z (7 months ago)
link to tio
  • Perl, 135 bytes
  • ---
  • Takes newline-separated list of Roman numerals as input. Outputs nothing unless one of the 50 strings is found, in which case it is printed:
  • ```
  • perl -ple'($_)=/CCCC|IIII|MMMM|XXXX|CDC|CM[CDM]|I[IX][VX]|I[VX]I|VI[VX]|V[VX]|XC[CLX]|XLX|[CDX]C[DM]|[DX][DM]|[ILV][CDLM]|[ILX]X[CL]/g'
  • ```
  • Minimising a regex is nontrivial but seems to be the bulk of any code. I expect it is possible to shrink it even more than this attempt.
  • * `-l` - strip newline from input and add it back on output
  • * `-p` - load each line of input as `$_`, manipulate, then print
  • * `-e` - program follows
  • * `/.../g` - return list of all matches
  • * `($_) = ...` - store first match in `$_` (if no matches, assigns empty string)
  • To print "VALID" instead of nothing increases code to 141 bytes:
  • ```
  • perl -ple'$_=/CCCC|IIII|MMMM|XXXX|CDC|CM[CDM]|I[IX][VX]|I[VX]I|VI[VX]|V[VX]|XC[CLX]|XLX|[CDX]C[DM]|[DX][DM]|[ILV][CDLM]|[ILX]X[CL]/?$&:VALID'
  • ```
  • * `$_ = /.../ ? $& : VALID` - ternary assignment to `$_` - if a match is found, assign its value (`$&`), otherwise assign bareword `VALID`
  • Perl, 135 bytes
  • ---
  • Takes newline-separated list of Roman numerals as input. Outputs nothing unless one of the 50 strings is found, in which case it is printed:
  • ```
  • perl -ple'($_)=/CCCC|IIII|MMMM|XXXX|CDC|CM[CDM]|I[IX][VX]|I[VX]I|VI[VX]|V[VX]|XC[CLX]|XLX|[CDX]C[DM]|[DX][DM]|[ILV][CDLM]|[ILX]X[CL]/g'
  • ```
  • Minimising a regex is nontrivial but seems to be the bulk of any code. I expect it is possible to shrink it even more than this attempt.
  • * `-l` - strip newline from input and add it back on output
  • * `-p` - load each line of input as `$_`, manipulate, then print
  • * `-e` - program follows
  • * `/.../g` - return list of all matches
  • * `($_) = ...` - store first match in `$_` (if no matches, assigns empty string)
  • To print "VALID" instead of nothing increases code to 141 bytes:
  • ```
  • perl -ple'$_=/CCCC|IIII|MMMM|XXXX|CDC|CM[CDM]|I[IX][VX]|I[VX]I|VI[VX]|V[VX]|XC[CLX]|XLX|[CDX]C[DM]|[DX][DM]|[ILV][CDLM]|[ILX]X[CL]/?$&:VALID'
  • ```
  • * `$_ = /.../ ? $& : VALID` - ternary assignment to `$_` - if a match is found, assign its value (`$&`), otherwise assign bareword `VALID`
  • [Try it online!][TIO-mmdh20oa]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mmdh20oa]: https://tio.run/##LVBNi9tADL3Prxjo0lza7KmXQilFugik6yAwpkmTYbNtapvY26Uwv73uk7MCjaSnp6@Z6u36aX2XJZ/HYbfkX8P4mi/QZcxPdcnL5XkO/3g61WnJv//m03iu@WU4XY7DUz1/yPOYZfen5jos9VbPqKj5UHcP37/s9/vdIU/H25KPc/75Mi/5cIcP@Ue9jq8rokeCNIE0gzSHNGJqZB2x9U068b4rHh5eaWWzrWyvU0caVr2B7z11UdTBuzuipUdC7773Dn7/@PXh/efyTYXXVVJJnjRR4mRJECIGAAQQsACBQqVApSAh0LAODSJBGbpxJZkyHnc0IBFPRnEWDMTRxEydoiMuJnMKihkjq@CVrUWQXUvaCFuN81uCUfFGMQY/RXNS9YSIzWIzZhbD@kXJpbhRccU6RQU/giW8mMat@BmchbGM6ljZIm@q0QUjUY9eiv@lhAWcIgPQtlvBZ0RRg1nMtkHYi8ns3zgtz@Mwrx@n638 "Perl 5 – Try It Online"
#1: Initial revision by user avatar jhnc‭ · 2026-03-05T04:35:49Z (7 months ago)
Perl, 135 bytes
---

Takes newline-separated list of Roman numerals as input. Outputs nothing unless one of the 50 strings is found, in which case it is printed:

```
perl -ple'($_)=/CCCC|IIII|MMMM|XXXX|CDC|CM[CDM]|I[IX][VX]|I[VX]I|VI[VX]|V[VX]|XC[CLX]|XLX|[CDX]C[DM]|[DX][DM]|[ILV][CDLM]|[ILX]X[CL]/g'
```

Minimising a regex is nontrivial but seems to be the bulk of any code. I expect it is possible to shrink it even more than this attempt.

* `-l` - strip newline from input and add it back on output
* `-p` - load each line of input as `$_`, manipulate, then print
* `-e` - program follows
* `/.../g` - return list of all matches
* `($_) = ...` - store first match in `$_` (if no matches, assigns empty string)

To print "VALID" instead of nothing increases code to 141 bytes:

```
perl -ple'$_=/CCCC|IIII|MMMM|XXXX|CDC|CM[CDM]|I[IX][VX]|I[VX]I|VI[VX]|V[VX]|XC[CLX]|XLX|[CDX]C[DM]|[DX][DM]|[ILV][CDLM]|[ILX]X[CL]/?$&:VALID'
```

* `$_ = /.../ ? $& : VALID` - ternary assignment to `$_` - if a match is found, assign its value (`$&`), otherwise assign bareword `VALID`