It is possible to filter items that fits a simple condition to match strings in Julia:
y = ["1 123","2512","31 12","1225"]
filter(x-> ' ' in x, y)
[out]:
2-element Array{String,1}:
"1 123"
"31 12"
But how do I get the reverse where I want to keep the items that doesn't match the condition in a filter?
This syntax isn't right:
> y = ["1 123","2512","31 12","1225"]
> filter(x-> !' ' in x, y)
MethodError: no method matching !(::Char)
Closest candidates are:
!(::Bool) at bool.jl:16
!(::BitArray{N}) at bitarray.jl:1036
!(::AbstractArray{Bool,N}) at arraymath.jl:30
...
in filter(::##93#94, ::Array{String,1}) at ./array.jl:1408
Neither is such Python-like one:
> y = ["1 123","2512","31 12","1225"]
> filter(x-> ' ' not in x, y)
syntax: missing comma or ) in argument list
Additionally, I've also tried to use a regex:
> y = ["1 123","2512","31 12","1225"]
> filter(x-> match(r"[\s]", x), y)
TypeError: non-boolean (RegexMatch) used in boolean context
in filter(::##95#96, ::Array{String,1}) at ./array.jl:1408
Beyond checking whether a whitespace is in string, how can I use the match()
with a regex to filter out items from a list of strings?
In order:
filter(x-> !' ' in x, y)
. The precedence is wrong here. The error message is telling you that it's trying to apply the !
function to a single Char
argument: (!' ') in x
. You need explicit parentheses:
julia> filter(x-> !(' ' in x), y)
2-element Array{String,1}:
"2512"
"1225"
filter(x-> ' ' not in x, y)
. not
isn't a keyword in Julia.
filter(x-> match(r"[\s]", x), y)
. The error is telling you that it expected a boolean value but didn't get one. Unlike Python, Julia doesn't have "truthy" values. So instead of match
, use contains
.
julia> filter(!contains(r"[\s]"), y)
2-element Vector{String}:
"2512"
"1225"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With