# How to find and replace (Regx) in string abcdac only 'a' who is not before 'c'?

**URL:** <https://discourse.julialang.org/t/how-to-find-and-replace-regx-in-string-abcdac-only-a-who-is-not-before-c/45354>\
**Category:** General Usage\
**Created:** [August 22, 2020, 8:41am UTC](https://discourse.julialang.org/t/how-to-find-and-replace-regx-in-string-abcdac-only-a-who-is-not-before-c/45354 "2020-08-22T08:41:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [August 22, 2020, 8:41am UTC](https://discourse.julialang.org/t/how-to-find-and-replace-regx-in-string-abcdac-only-a-who-is-not-before-c/45354/1 "2020-08-22T08:41:13Z")

</div>

Ho to find and repalce (Regx) in string abcdac only a who is not before c ?  
Paul

---

<div class="post-metadata">

**Author:** ![gsoleilhac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gsoleilhac/32/3480_2.png) [@gsoleilhac](https://discourse.julialang.org/u/gsoleilhac)\
**Post date:** [August 22, 2020, 9:53am UTC](https://discourse.julialang.org/t/how-to-find-and-replace-regx-in-string-abcdac-only-a-who-is-not-before-c/45354/2 "2020-08-22T09:53:13Z")

</div>

Are you having trouble finding a regex to do that ? or using julia to replace a regex match in a string ?

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [August 22, 2020, 10:11am UTC](https://discourse.julialang.org/t/how-to-find-and-replace-regx-in-string-abcdac-only-a-who-is-not-before-c/45354/3 "2020-08-22T10:11:45Z")

</div>

If the issue is with finding the proper regex, [regex101.com](http://regex101.com) is usually an invaluable resource.

Also: [[ANN] ReadableRegex.jl](https://discourse.julialang.org/t/ann-readableregex-jl/43450) might help

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 22, 2020, 10:41am UTC](https://discourse.julialang.org/t/how-to-find-and-replace-regx-in-string-abcdac-only-a-who-is-not-before-c/45354/4 "2020-08-22T10:41:53Z")

</div>

```julia
julia> s="aabcacab"
"aabcacab"

julia> rx=r"a[^c]"
r"a[^c]"

julia> for m in eachmatch(rx,s;overlap=true)
       println(m,m.offset)
       end
RegexMatch("aa", 1="a")1
RegexMatch("ab", 1="a")2
RegexMatch("ab", 1="a")7

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 22, 2020, 11:14am UTC](https://discourse.julialang.org/t/how-to-find-and-replace-regx-in-string-abcdac-only-a-who-is-not-before-c/45354/6 "2020-08-22T11:14:57Z")

</div>

A “a” at the end of the string is not yet matched, so:

```julia
julia> s="aabc acaba"
"aabc acaba"

julia> rx=r"(a[^c])|(a$)"
r"(a[^c])|(a$)"

julia> for m in eachmatch(rx,s;overlap=true)
       println(m,m.offset)
       end
RegexMatch("aa", 1="aa", 2=nothing)1
RegexMatch("ab", 1="ab", 2=nothing)2
RegexMatch("ab", 1="ab", 2=nothing)8
RegexMatch("a", 1=nothing, 2="a")10

julia> sa=split(s,"");

julia> for m in eachmatch(rx,s;overlap=true)
              sa[m.offset]="x"
              end

julia> new_s=join(sa)
"xxbc acxbx"

```

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [August 22, 2020, 12:34pm UTC](https://discourse.julialang.org/t/how-to-find-and-replace-regx-in-string-abcdac-only-a-who-is-not-before-c/45354/7 "2020-08-22T12:34:16Z")

</div>

Sounds like you want a “negative lookahead assertion”: the expression `(?!......)` means “don’t match the following, and don’t make it part of the capture”. So in your case, `r"a(?!c)"` is what you are looking for.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 22, 2020, 2:11pm UTC](https://discourse.julialang.org/t/how-to-find-and-replace-regx-in-string-abcdac-only-a-who-is-not-before-c/45354/8 "2020-08-22T14:11:42Z")

</div>

> [@tkluck](#):
>
> So in your case, `r"a(?!c)"` is what you are looking for.

And now replace works better:

```julia
julia> rx=r"a(?!c)"
r"a(?!c)"

julia> s="aabc acaba"
"aabc acaba"

julia> replace(s,rx=>"x")
"xxbc acxbx"

```
