# Is there a way to keep the delimiter in split function, as a separate element?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-keep-the-delimiter-in-split-function-as-a-separate-element/51995>\
**Category:** General Usage\
**Tags:** strings, regex\
**Created:** [December 17, 2020, 5:01pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-keep-the-delimiter-in-split-function-as-a-separate-element/51995 "2020-12-17T17:01:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![D\_A](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@D\_A](https://discourse.julialang.org/u/D_A)\
**Post date:** [December 17, 2020, 5:01pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-keep-the-delimiter-in-split-function-as-a-separate-element/51995/1 "2020-12-17T17:01:53Z")

</div>

Hello,

This is identical to [this](https://stackoverflow.com/a/15668433).

In my case, if I want to split a string (for example a quite complex anthroponym, with dashes, apostrophes, etc.), making complex capitalization depending on these punctuation delimiter, I don’t find an easy way to keep all elements in order, as I could in other languages.

MWE:

```julia
> split("123.456.789", r"\.")
["123", "456", "789"] # Current and expected behaviour.

> split("123.456.789", r"(\.)")
["123", ".", "456", ".", "789"] # Expected behaviour because the pattern is inside a capture group.

> split("AAA’s BBB-CCC", r"([’'\s-])") # More complex example.
["AAA", "s", "BBB", "CCC"] # Current behaviour.
["AAA", "’", "s", " ", "BBB", "-", "CCC"] # Expected behaviour.

> split("123.456.789", r"\."; keep=true) # Kind of enhancement with retrocompatibility.
["123", ".", "456", ".", "789"]

```

This is quite similar to [this ticket](https://github.com/JuliaLang/julia/issues/20625), but the main difference is to keep the delimiter as a separate element among others (but the `keep` argument could be a string like `separate`, `previous` or `next` to be useful for all cases…).

First, I planned to open a new ticket on GitHub, but maybe I missed something!

Sincerely.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [December 17, 2020, 5:31pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-keep-the-delimiter-in-split-function-as-a-separate-element/51995/2 "2020-12-17T17:31:54Z")

</div>

I guess you’re looking for a Base Julia way to do this, but in case you just want to get your problem solved, you can do it with lookaheads and lookbehinds. I have a package `ReadableRegex` which makes this a bit nicer to read:

```julia
using ReadableRegex

function split_keeping_splitter(string, splitter)
    r = Regex(
        either(
            look_for("", before = splitter),
            look_for("", after = splitter)
        )
    )
    split(string, r)
end

split_keeping_splitter("123.456.789", ".")

```

```julia
5-element Vector{SubString{String}}:
 "123"
 "."
 "456"
 "."
 "789"

```

The regex being constructed for the “.” version would be `r"(?:(?:(?<=\.)(?:))|(?:(?:)(?=\.)))"` so I can’t recommend constructing that yourself 😉

---

<div class="post-metadata">

**Author:** ![D\_A](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@D\_A](https://discourse.julialang.org/u/D_A)\
**Post date:** [December 17, 2020, 8:15pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-keep-the-delimiter-in-split-function-as-a-separate-element/51995/3 "2020-12-17T20:15:41Z")

</div>

Thank you for your answer!

Yes, I was searching for a Base Julia way of doing it, but your proposal is faster than what I was planning to do: `r"(?<=X)|(?=X)"` is a pretty good way to solve the issue (just a regex to change, no more lines!), while waiting for a potential Base improvement (for ease of use, maybe also performance).

If I don’t have other answers soon, I will mark your answer as _solution_ and open a ticket on Github…
