-3

I have 2 arrays like below.I only need those elements in $b whose names are not present in $a.As it can be seen,element names on $a and $b are not same.$b has full name whereas $a has only a part of the name.

$a="rohit","shikar","virat","ravindra"
$b="rohit : sharma","shikar : dhawan","virat : kohli","ajinx : rahane","ravi : ashwin"

I've tried to loop through $b and use operators like -contain,-match,-like but those do not work.

2
  • 2
    Please do not double post. You asked practically the same question yesterday and got some good answers. Commented Mar 26, 2015 at 13:06
  • Sorry about that.I only consumed the answer (using the -contains operator)from my earlier question.In that sense,this one appeared a different case to me as those operators do not work in this example.Thanks to all those who helped me with this problem. Commented Mar 27, 2015 at 11:47

1 Answer 1

0

Using a regex:

$a="rohit","shikar","virat","ravindra"
$b="rohit : sharma","shikar : dhawan","virat : kohli","ajinx : rahane","ravi : ashwin"

$a_regex = ‘(?i)(‘ + (($a |foreach {[regex]::escape($_)}) –join “|”) + ‘)’

$b -notmatch $a_regex

ajinx : rahane
ravi : ashwin

If you know there aren't going to be an regex metacharacters in $a that might need to be escaped, it can be as simple as:

$a="rohit","shikar","virat","ravindra"
$b="rohit : sharma","shikar : dhawan","virat : kohli","ajinx : rahane","ravi : ashwin"

$a_regex = $a -join '|'

$b -notmatch $a_regex

ajinx : rahane
ravi : ashwin
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.