I have large StringExpression
that is used in StringCases
. Some of the parts being pulled out are integer strings. These pull out fine. However, when I go to convert them from strings to integers with FromDigits
(it can take a string of integers) in StringCases
it errors but still converts the string to an integer. Minimal example:
ClearAll[BigStringExpr];
Off[RuleDelayed::rhs];
BigStringExpr[anInteger_Symbol] :=
StartOfString ~~
anInteger : (Except["0", DigitCharacter] ~~ DigitCharacter ...) ~~
EndOfString
On[RuleDelayed::rhs];
StringCases["1234", BigStringExpr[y] -> y]
(* {"1234"} *)
StringCases["1234", BigStringExpr[y] -> FromDigits@y]
FromDigits::nlst: The expression y is not a list of digits or a string of valid digits. >>
(* {1234} *)
Any ideas why this is happening? I know that I can convert after StringCases
but I would prefer to keep it in there since it is a simple call and also helps with code readability.
RuleDelayed
(:>
) rather thanRule
(->
) in yourStringCases
; this will force evaluation of yourFromDigits
function at the opportune time, i.e. when $y$ has been given a value. $\endgroup${x, x} /. x -> RandomReal[]
verses{x, x} /. x :> RandomReal[]
. Thanks. $\endgroup$