I want to validate the string to be an integer of such format of undefined length.
/\A (?<d> ( ( (\g<d>[[:space:]])? \d)? \d)? \d) \z/x === "12 123 123"
but it throws
SyntaxError: never ending recursion
I want to validate the string to be an integer of such format of undefined length.
/\A (?<d> ( ( (\g<d>[[:space:]])? \d)? \d)? \d) \z/x === "12 123 123"
but it throws
SyntaxError: never ending recursion
It seems you can use
/\A\d{1,3}(?: \d{3})*\z/
Or, to support any kind of whitespace:
/\A\d{1,3}(?:[[:space:]]\d{3})*\z/
/\A\d{1,3}(?:\p{Z}\d{3})*\z/
Details:
\A
- start of a string\d{1,3}
- one, two or three digits(?:[[:space:]]\d{3})*
- zero or more occurrences of a whitespace and then three digits\z
- end of string.See the Rubular demo.
Also, see Fastest way to check if a string matches a regexp in ruby? how to check if a string matches a regex in Ruby. In short, it is your_pattern.match?(your_string)
.
© 2022 - 2024 — McMap. All rights reserved.
/\A\d{1,3}(?: \d{3})*\z/
? – Triley===
is discouraged for regex matching. Use=~
instead. – Lafreniere\A(?<d>\g<d>)\z
then you define the group, and immediately refer to the expression in the defined group and will keep on looping. There is no exit clause. – Bargainbasement