# Subtring match in array\<str\> info column

**URL:** <https://discuss.hail.is/t/subtring-match-in-array-str-info-column/2383>\
**Category:** Hail Query & hailctl\
**Created:** [November 30, 2021, 1:47pm UTC](https://discuss.hail.is/t/subtring-match-in-array-str-info-column/2383 "2021-11-30T13:47:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![syaffers](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/syaffers/32/675_2.png) [@syaffers](https://discuss.hail.is/u/syaffers)\
**Post date:** [November 30, 2021, 1:47pm UTC](https://discuss.hail.is/t/subtring-match-in-array-str-info-column/2383/1 "2021-11-30T13:47:50Z")

</div>

I have a MT that looks something like

```auto
+---------------+---------------+----------------------+
| locus | alleles | info.CLNDISDB |
+---------------+---------------+----------------------+
| locus<GRCh37> | array<str> | array<str> |
+---------------+---------------+----------------------+
| 2:47630512 | ["A","AG"] | ["A", "A|B"] |
| 2:47690234 | ["T","TAATG"] | ["B"] |
| 2:47693860 | ["T","TA"] | ["A"] |
| 2:47705430 | ["TTAA","T"] | ["B|C", "C|D"] |
| 2:48026310 | ["C","CTA"] | ["B", "A|C"] |
+---------------+---------------+----------------------+

```

I want to filter all the rows that contain the string `'B'`. My current query uses the `.contains()` function as follows:

```python
mt.filter_rows(
    (~hl.is_missing(mt.info['CLNDISDB'])) &
    (mt.info['CLNDISDB'].contains('B'))
)

```

But I only get rows 2 and 5 when I want to get all 4 rows that matches `'B'` somehwere in the info (i.e. rows 1, 2, 4 and 5). Is there a way to match within the `.contains()` function? Say a substring or regex match into a string array expression?

---

<div class="post-metadata">

**Author:** ![johnc1231](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/johnc1231/32/286_2.png) [@johnc1231](https://discuss.hail.is/u/johnc1231)\
**Post date:** [November 30, 2021, 1:53pm UTC](https://discuss.hail.is/t/subtring-match-in-array-str-info-column/2383/2 "2021-11-30T13:53:16Z")

</div>

So you don’t want to be calling the `ArrayExpression.contains` method, which is checking if any element of the array exactly matches. You want to be calling the `StringExpression.contains` method on each element of the array. I think: `mt.info['CLNDISDB'].any(lambda element: element.contains("B")` should do it.

---

<div class="post-metadata">

**Author:** ![syaffers](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/syaffers/32/675_2.png) [@syaffers](https://discuss.hail.is/u/syaffers)\
**Post date:** [November 30, 2021, 4:30pm UTC](https://discuss.hail.is/t/subtring-match-in-array-str-info-column/2383/3 "2021-11-30T16:30:55Z")

</div>

Yeah, this is exactly what I need for this particular use case. I’m guessing we can also apply some form of regex on the strings too right? Instead of just substring matches?

---

<div class="post-metadata">

**Author:** ![johnc1231](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/johnc1231/32/286_2.png) [@johnc1231](https://discuss.hail.is/u/johnc1231)\
**Post date:** [November 30, 2021, 4:37pm UTC](https://discuss.hail.is/t/subtring-match-in-array-str-info-column/2383/4 "2021-11-30T16:37:48Z")

</div>

Yup, instead of `StringExpression.contains` you can use `StringExpression.matches`. See: [Hail | StringExpression](https://hail.is/docs/0.2/hail.expr.StringExpression.html#hail.expr.StringExpression.matches)
