# Broadcasting Regex in DataFrame to create new column

**URL:** <https://discourse.julialang.org/t/broadcasting-regex-in-dataframe-to-create-new-column/40029>\
**Category:** New to Julia\
**Tags:** question, regex, dataframes, broadcasting\
**Created:** [May 23, 2020, 4:03pm UTC](https://discourse.julialang.org/t/broadcasting-regex-in-dataframe-to-create-new-column/40029 "2020-05-23T16:03:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petra/32/15124_2.png) [@Petra](https://discourse.julialang.org/u/Petra)\
**Post date:** [May 23, 2020, 4:03pm UTC](https://discourse.julialang.org/t/broadcasting-regex-in-dataframe-to-create-new-column/40029/1 "2020-05-23T16:03:33Z")

</div>

I’m quite new to Julia using Python and R before. I want to populate a new column in a DataFrame with data extracted from an existing column using Regex. I apply the function **match** , broadcasting and a regex to extract the information. I can populate the new column with the RegexMatch object, only. I want to get the string from the RegexMatch object. But, the function **m.match** ([https://docs.julialang.org/en/v1/manual/strings/#Regular-Expressions-1](https://docs.julialang.org/en/v1/manual/strings/#Regular-Expressions-1)) doesn’t work in combination with broadcasting.

```julia
using DataFrames
using Random
series = rand(["110-bla","599-tag"],6)
test = DataFrame(s1 = series)
test.s2 = match.(r"[0-9]*",test.s1)

```

result=  
Row │ s1 │ s2 │  
│ │ String │ RegexMatch │  
├─────┼─────────┼───────────────────┤  
│ 1 │ 110-bla │ RegexMatch(“110”) │  
│ 2 │ 599-tag │ RegexMatch(“599”) │  
│ 3 │ 110-bla │ RegexMatch(“110”) │  
│ 4 │ 599-tag │ RegexMatch(“599”) │  
│ 5 │ 110-bla │ RegexMatch(“110”) │  
│ 6 │ 110-bla │ RegexMatch(“110”)

for a single value I can extract the string (here “110”) correctly

```julia
match(r"[0-9]*","110-bla").match

```

but when I try to broadcast, I get an error message “type Array has no field match”

```julia
test.s2 = match.(r"[0-9]*",test.s1).match

```

Thanks a lot in advance for your help!

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 23, 2020, 4:07pm UTC](https://discourse.julialang.org/t/broadcasting-regex-in-dataframe-to-create-new-column/40029/2 "2020-05-23T16:07:48Z")

</div>

You can broadcast `getproperty` like so

```julia
getproperty.(match.(r"[0-9]*",test.s1), :match)

```

Another alternative is to define a function

```julia
getmatch(x) = x.match
getmatch.(match.(r"[0-9]*",test.s1))

```

---

<div class="post-metadata">

**Author:** ![Petra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petra/32/15124_2.png) [@Petra](https://discourse.julialang.org/u/Petra)\
**Post date:** [May 23, 2020, 4:10pm UTC](https://discourse.julialang.org/t/broadcasting-regex-in-dataframe-to-create-new-column/40029/3 "2020-05-23T16:10:08Z")

</div>

Thanks very much for the swift reply. Your solution works perfectly! Petra

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [May 24, 2020, 12:56am UTC](https://discourse.julialang.org/t/broadcasting-regex-in-dataframe-to-create-new-column/40029/4 "2020-05-24T00:56:31Z")

</div>

Just to offer another solution that’s not broadcasting:

```julia
test.s2 = map (test.s1) do s
    m = match(r"[0-9]*",s)
    m.match
end

```

I offer this because, as neat as broadcasting is, I find I’m easily tripped up when things get complicated, or I’m using a container that shouldn’t be broadcasted into (eg `in.(xs, some_set)` will try to broadcast through the set as well, when I usually want to check if each element of `xs` is in the set).
