How to make a pattern in Lua that matches only when no number (normal or roman) is present?

215 views Asked by At

I want to make a pattern that matches only when a string contains no arabic or roman numerals (note that the roman numerals can only go up to X (10))

Examples:

"Rock"                 <-- matches
"Rock IV"              <-- doesn't match
"Rock 4"               <-- doesn't match
"Rock Paper"           <-- matches
"Rock Paper Scissors"  <-- matches
"Rock Paper VIII"      <-- doesn't match
"Rock Paper 8"         <-- doesn't match

My current code only allows for the usage of 1 word and a numeral. My goal is to make it so it only doesn't match when a numeral is present. Code:

"^([^%s]+)$"

As the title says, I am using Lua. I can also note that the code will be used on one of the fandom wikis, so syntax should match Scribunto syntax.

1

There are 1 answers

0
Piglet On

There is no pattern I can think of that would match what you need in one go with any possible word.

".*%s?[^IVXLCDM0-9]+$"

This matches any string that does not end with space and digits.

This would fail for words that with Roman digits like "RockM".

Often it is easier to match something that to not match something.

If you just want to check it might be easier to search the string for [0-9IVXLCDM]