Can I match an or expression in regex multiple times?
All you need is to add a dash to the character set that follows the big group there. It's currently [\s]
; change it to [\s-]
:
(\$[\s]?([\d\,]+)([\.\d*](\d*))?([\s]?(hundred|thousand|million|billion|trillion))?)|((((one|two|three|four|four|five|five|six|six|seven|seven|eight|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|eightteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|hundred|third|quarter|fifth|half|hundred|thousand|million|billion|trillion)[\s-]?)+)?([\d\.\,\"a"]+)?([\s]*(dollars|dollar|USD)))
^
https://regex101.com/r/1TQLf2/
To match "...Canadian dollars" in addition to "...dollars", add "Canadian dollars" to your current group of (dollars|dollar|USD)
. Change to
(Canadian dollars|dollars|dollar|USD)
(maybe ([Cc]anadian dollars|...
if you don't use /i
)
Give you this gift
(?:USD *)?(?:(?P<num1>\$(?:\d+[,.]?)+)(?: *(?P<mod1>(?:(?:hundred|thousand|million|billion|trillion)[ -]*)+)? *(?:(?P<badge1>(canad(?:ian|a) *)?dollars?\b|cent[s.]?\b)?))|(?:(?P<num2>(?:\d+[,. ]?)+)|(?P<txt>\b(?:(?:one|quarter|two|ninety|five|a|half|one|three|twenty)[ -]??)+))(?: *(?P<mod2>(?:(?:hundred|thousand|million|billion|trillion)[ -]*?)+ *)?(?:(?P<badge2>(canad(?:ian|a) *)?dollars?|cent[s.]?)\b)))(?: */ *dollar)?
body {
margin: 0;
height: 100vh;
}
iframe {
height: 100vh;
}
<iframe frameborder="0"
width="100%" height="100vh"
src="https://vquilon.github.io/beyond-regex/panels/visualizer#!embed=true&flags=img&re=(%3F%3AUSD%20*)%3F(%3F%3A(%3FP%3Cnum1%3E%5C%24(%3F%3A%5Cd%2B%5B%2C.%5D%3F)%2B)(%3F%3A%20*(%3FP%3Cmod1%3E(%3F%3A(%3F%3Ahundred%7Cthousand%7Cmillion%7Cbillion%7Ctrillion)%5B%20-%5D*)%2B)%3F%20*(%3F%3A(%3FP%3Cbadge1%3E(canad(%3F%3Aian%7Ca)%20*)%3Fdollars%3F%5Cb%7Ccent%5Bs.%5D%3F%5Cb)%3F))%7C(%3F%3A(%3FP%3Cnum2%3E(%3F%3A%5Cd%2B%5B%2C.%20%5D%3F)%2B)%7C(%3FP%3Ctxt%3E%5Cb(%3F%3A(%3F%3Aone%7Cquarter%7Ctwo%7Cninety%7Cfive%7Ca%7Chalf%7Cone%7Cthree%7Ctwenty)%5B%20-%5D%3F%3F)%2B))(%3F%3A%20*(%3FP%3Cmod2%3E(%3F%3A(%3F%3Ahundred%7Cthousand%7Cmillion%7Cbillion%7Ctrillion)%5B%20-%5D*%3F)%2B%20*)%3F(%3F%3A(%3FP%3Cbadge2%3E(canad(%3F%3Aian%7Ca)%20*)%3Fdollars%3F%7Ccent%5Bs.%5D%3F)%5Cb)))(%3F%3A%20*%2F%20*dollar)%3F&reLang=python">
</iframe>