# Use regex replacement strings

**URL:** <https://discourse.julialang.org/t/use-regex-replacement-strings/76729>\
**Category:** New to Julia\
**Tags:** regex\
**Created:** [February 19, 2022, 6:53am UTC](https://discourse.julialang.org/t/use-regex-replacement-strings/76729 "2022-02-19T06:53:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mo-Gul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mo-gul/32/33308_2.png) [@Mo-Gul](https://discourse.julialang.org/u/Mo-Gul)\
**Post date:** [February 19, 2022, 6:53am UTC](https://discourse.julialang.org/t/use-regex-replacement-strings/76729/1 "2022-02-19T06:53:15Z")

</div>

I want to transform a string containing a `+` with different variants. One of them is removing the `+` and transforming the following letter to lowercase. Currently I do this in two steps.

```julia
str = "Abdul+Hakim";
str2 = replace(str, r"\+(\p{Lu})" => lowercase);
replace(str2, "+" => "")

```

which results in

```julia
"Abdulhakim"

```

Is there a way to do this in one go? With only knowing basics of regular expressions I thought that this in principle should be possible when I have a look at

[https://www.regular-expressions.info/replacecase.html](https://www.regular-expressions.info/replacecase.html)

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [February 19, 2022, 7:31am UTC](https://discourse.julialang.org/t/use-regex-replacement-strings/76729/2 "2022-02-19T07:31:55Z")

</div>

It seems like pcre2 has no case switching, so your best option is

```julia
lowercase(replace(str, '+' => ""))

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [February 19, 2022, 7:34am UTC](https://discourse.julialang.org/t/use-regex-replacement-strings/76729/3 "2022-02-19T07:34:16Z")

</div>

If there was, it would have been

```julia
/(.+)\+(.+)/\L\1\2\E/

```

or

```julia
/(.+)\+(.+)/\L1\L2/

```

---

<div class="post-metadata">

**Author:** ![Mo-Gul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mo-gul/32/33308_2.png) [@Mo-Gul](https://discourse.julialang.org/u/Mo-Gul)\
**Post date:** [February 19, 2022, 7:34am UTC](https://discourse.julialang.org/t/use-regex-replacement-strings/76729/4 "2022-02-19T07:34:42Z")

</div>

Unfortunately, that doesn’t work because then you get

```julia
"abdulhakim"

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [February 19, 2022, 7:38am UTC](https://discourse.julialang.org/t/use-regex-replacement-strings/76729/5 "2022-02-19T07:38:32Z")

</div>

My bad, I misread the specification.

Yes, I do think the two step process is the best you can do then.

---

<div class="post-metadata">

**Author:** ![Mo-Gul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mo-gul/32/33308_2.png) [@Mo-Gul](https://discourse.julialang.org/u/Mo-Gul)\
**Post date:** [February 19, 2022, 8:12am UTC](https://discourse.julialang.org/t/use-regex-replacement-strings/76729/6 "2022-02-19T08:12:27Z")

</div>

Thank’s. I thought so, but maybe I missed something or there is a package that has this functionality …

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 19, 2022, 8:12am UTC](https://discourse.julialang.org/t/use-regex-replacement-strings/76729/7 "2022-02-19T08:12:45Z")

</div>

You could use the `titlecase()` function:

```julia
titlecase(replace(str, '+' => ""))

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [February 19, 2022, 8:15am UTC](https://discourse.julialang.org/t/use-regex-replacement-strings/76729/8 "2022-02-19T08:15:06Z")

</div>

```julia
julia> replace(str, r"\+\p{Lu}" => x -> lowercase(x[2]))
"Abdulhakim"

```

---

<div class="post-metadata">

**Author:** ![Mo-Gul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mo-gul/32/33308_2.png) [@Mo-Gul](https://discourse.julialang.org/u/Mo-Gul)\
**Post date:** [February 19, 2022, 8:23am UTC](https://discourse.julialang.org/t/use-regex-replacement-strings/76729/9 "2022-02-19T08:23:08Z")

</div>

Many thanks for the answers. While [@rafael.guerra’s answer](https://discourse.julialang.org/t/use-regex-replacement-strings/76729/7) works in this case, because the initial letter is also uppercase, the more general solution is the one of [@aplavin](https://discourse.julialang.org/t/use-regex-replacement-strings/76729/8) which really only changes the matched part. Using an anonymous function … I would have never come up with that solution 🙂

You and Julia are awesome.
