8

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?

2
  • the expected output is ["2512", "1225"] , right? Commented Feb 7, 2017 at 7:03
  • yep. it's ["2512", "1225"] but the question is more generic asking about how to use filter() with match() conditions. Commented Feb 7, 2017 at 7:05

1 Answer 1

12

In order:

  1. 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" 
  2. filter(x-> ' ' not in x, y). not isn't a keyword in Julia.

  3. 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" 
Sign up to request clarification or add additional context in comments.

4 Comments

Cool, BTW, does Julia have Noney values?
Nope, just true and false.
In Julia 1.0 this would be filter(x -> match(r"[\s]", x) === nothing, y)
Slightly simpler is filter(!contains(r"[\s]"), y).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.