I can use occursin
function, but its haystack
argument cannot be a regular expression, which means I have to pass the entire alphanumeric string to it. Is there a neat way of doing this in Julia?
CodePudding user response:
I'm not sure your assumption about occursin
is correct:
julia> occursin(r"[a-zA-z]", "ABC123")
true
julia> occursin(r"[a-zA-z]", "123")
false
CodePudding user response:
but its
haystack
argument cannot be a regular expression, which means I have to pass the entire alphanumeric string to it.
If you mean its needle
argument, it can be a Regex, for eg.:
julia> occursin(r"^[[:alpha:][:digit:]]*$", "adf24asg24y")
true
julia> occursin(r"^[[:alpha:][:digit:]]*$", "adf24asg2_4y")
false
This checks that the given haystack
string is alphanumeric using Unicode-aware character classes
[:alpha:]
and [:digit:]
which you can think of as equivalent to a-zA-Z
and \d
respectively, extended to non-English characters too. (As always with Unicode, a "perfect" solution involves more work and complication, but this takes you most of the way.)
If you do mean you want the haystack
argument to be a Regex, it's not clear why you'd want that here, and also why "I have to pass the entire alphanumeric string to it" is a bad thing.