Ho to find and repalce (Regx) in string abcdac only a who is not before c ?
Paul
Are you having trouble finding a regex to do that ? or using julia to replace a regex match in a string ?
If the issue is with finding the proper regex, regex101.com is usually an invaluable resource.
Also: [ANN] ReadableRegex.jl might help
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
A “a” at the end of the string is not yet matched, so:
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"
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.
And now replace works better:
julia> rx=r"a(?!c)"
r"a(?!c)"
julia> s="aabc acaba"
"aabc acaba"
julia> replace(s,rx=>"x")
"xxbc acxbx"