# Remove unmatched parts via regular expression

**URL:** <https://discourse.julialang.org/t/remove-unmatched-parts-via-regular-expression/73850>\
**Category:** New to Julia\
**Tags:** strings, regex\
**Created:** [December 31, 2021, 4:07am UTC](https://discourse.julialang.org/t/remove-unmatched-parts-via-regular-expression/73850 "2021-12-31T04:07:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)\
**Post date:** [December 31, 2021, 4:07am UTC](https://discourse.julialang.org/t/remove-unmatched-parts-via-regular-expression/73850/1 "2021-12-31T04:07:58Z")

</div>

Hi,  
For example as below, how can I get the result “123” without “abc, space, …etc.”, using regular expression?

```julia
julia> replace("abc12 3abc ", r"\d+" => "")
"abc abc "

```

Technically, I can meet the aim via filter as below:

```julia
julia> filter(isdigit, "abc12 3abc ")
"123"

```

Just wonder if I can do it via regular expression. Thank you!

---

<div class="post-metadata">

**Author:** ![Matthew](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Matthew](https://discourse.julialang.org/u/Matthew)\
**Post date:** [December 31, 2021, 4:46am UTC](https://discourse.julialang.org/t/remove-unmatched-parts-via-regular-expression/73850/2 "2021-12-31T04:46:35Z")

</div>

`replace("abc12 3abc ", r"[a-z]+| " => "")`

---

<div class="post-metadata">

**Author:** ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)\
**Post date:** [December 31, 2021, 4:55am UTC](https://discourse.julialang.org/t/remove-unmatched-parts-via-regular-expression/73850/3 "2021-12-31T04:55:26Z")

</div>

if there’re others in the string, how can do it? Thanks!

```julia
julia> replace("abc12 3abc @-", r"[a-z]+| " => "")
"123@-"

```

---

<div class="post-metadata">

**Author:** ![Matthew](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Matthew](https://discourse.julialang.org/u/Matthew)\
**Post date:** [December 31, 2021, 5:05am UTC](https://discourse.julialang.org/t/remove-unmatched-parts-via-regular-expression/73850/4 "2021-12-31T05:05:17Z")

</div>

`replace(str, r"[a-z]+|@|-| " => "")`

Should work, though there may be a better method. It’s probably worth messing around with the [regex](https://en.wikipedia.org/wiki/Regular_expression) patterns!

---

<div class="post-metadata">

**Author:** ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)\
**Post date:** [December 31, 2021, 5:16am UTC](https://discourse.julialang.org/t/remove-unmatched-parts-via-regular-expression/73850/5 "2021-12-31T05:16:10Z")

</div>

Thanks. Agree that there should have a better way to remove anything (e.g., a-z, @, -, #, …, etc) except a matched pattern. I guess something like ‘!’ or ‘not’.

---

<div class="post-metadata">

**Author:** ![lbilli](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@lbilli](https://discourse.julialang.org/u/lbilli)\
**Post date:** [December 31, 2021, 5:28am UTC](https://discourse.julialang.org/t/remove-unmatched-parts-via-regular-expression/73850/6 "2021-12-31T05:28:38Z")

</div>

You can try:

```julia
julia> replace("abc12 3abc @-", r"[^\d]" => "")
"123"

```

---

<div class="post-metadata">

**Author:** ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)\
**Post date:** [December 31, 2021, 5:30am UTC](https://discourse.julialang.org/t/remove-unmatched-parts-via-regular-expression/73850/7 "2021-12-31T05:30:37Z")

</div>

that’s it. 👍
