# Read single quoted values inside strings

**URL:** <https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 15, 2023, 12:29pm UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877 "2023-05-15T12:29:48Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![uwbanjoman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwbanjoman/32/9998_2.png) [@uwbanjoman](https://discourse.julialang.org/u/uwbanjoman)\
**Post date:** [May 15, 2023, 12:29pm UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/1 "2023-05-15T12:29:48Z")

</div>

Hi all, I have opened a text file using the readlines() function. This text file has various sections containing different information. In some of the lines i am finding strings that contain information inside single quotes. There are multiple words within these quotes. how can i keep ‘THIS TEXT’ intact when splitting following string `"#1 1 0 'THIS TEXT' '' '88218681' 0.398 1 '' 1 1 2 0 168384 461330 0 "` at every “” ? splitting up at every space breaks the single quoted text in two.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 15, 2023, 12:36pm UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/2 "2023-05-15T12:36:39Z")

</div>

I believe that if you were using double quotes instead (as it is usual) maybe you could use [`DelimitedFile.readdlm`](https://docs.julialang.org/en/v1/stdlib/DelimitedFiles/#DelimitedFiles.readdlm-Tuple%7BAny,%20AbstractChar,%20Type,%20AbstractChar%7D).

---

<div class="post-metadata">

**Author:** ![uwbanjoman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwbanjoman/32/9998_2.png) [@uwbanjoman](https://discourse.julialang.org/u/uwbanjoman)\
**Post date:** [May 15, 2023, 12:40pm UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/3 "2023-05-15T12:40:19Z")

</div>

Thank you Henrique, but that is exactly my problem. The textfile is an export from some application which i can not change.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 15, 2023, 1:07pm UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/4 "2023-05-15T13:07:49Z")

</div>

Cannot you read the whole file to a String, replace all single quotes by double quotes, and then write it to a string buffer to pass it to `DelimitedFiles.readdlm`?

---

<div class="post-metadata">

**Author:** ![uwbanjoman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwbanjoman/32/9998_2.png) [@uwbanjoman](https://discourse.julialang.org/u/uwbanjoman)\
**Post date:** [May 15, 2023, 1:13pm UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/5 "2023-05-15T13:13:19Z")

</div>

That’s worth a try, Thanks

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [May 15, 2023, 2:43pm UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/6 "2023-05-15T14:43:55Z")

</div>

Ugly … but seems to work on your test line:

```julia
function tokenize(s)
    outbuffer = []

    buffer = []
    quoted = false

    for c in s
        if quoted
            if c == '\''
                push!(outbuffer, join(buffer))
                quoted = false
                buffer = []
            else
                push!(buffer, c)
            end
        elseif isspace(c)
            !iszero(length(buffer)) && push!(outbuffer, join(buffer))
            buffer = []
        else
            if c == '\''
                quoted = true
                continue
            end
            push!(buffer, c)
        end
    end

    outbuffer
end

```

```julia
test = "#1 1 0 'THIS TEXT' '' '88218681' 0.398 1 '' 1 1 2 0 168384 461330 0 "
tokenize(test) = Any["#1", "1", "0", "THIS TEXT", "", "88218681", "0.398", "1", "", "1", "1", "2", "0", "168384", "461330", "0"]

```

---

<div class="post-metadata">

**Author:** ![uwbanjoman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwbanjoman/32/9998_2.png) [@uwbanjoman](https://discourse.julialang.org/u/uwbanjoman)\
**Post date:** [May 15, 2023, 3:25pm UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/7 "2023-05-15T15:25:00Z")

</div>

wonderfull, this works!, I’ve tested it on another string as well. Thanks so much.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 15, 2023, 5:50pm UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/8 "2023-05-15T17:50:38Z")

</div>

Just to point it out, the code above, nor `DelimitedFiles.readdlm`, support escaping quotes inside a quoted field. I would suggest checking if this is a possibility.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 15, 2023, 6:32pm UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/9 "2023-05-15T18:32:47Z")

</div>

```julia
line1="#1 1 0 'THIS TEXT' '' '88218681' 0.398 1 '' 1 1 2 0 168384 461330 0 "

split(replace(line1, r"(\pL) (\pL)"=>s"\1_\2"))

16-element Vector{SubString{String}}:
 "#1"
 "1"
 "0"
 "'THIS_TEXT'"
 "''"
 "'88218681'"
 "0.398"
 "1"
 "''"
 "1"
 "1"
 "2"
 "0"
 "168384"
 "461330"
 "0"

```

```julia
split(replace(line1, r"(\pL) (\pL)"=>s"\1_\2","'"=>""))
14-element Vector{SubString{String}}:
 "#1"
 "1"
 "0"
 "THIS_TEXT"
 "88218681"
 "0.398"
 "1"
 "1"
 "1"
 "2"
 "0"
 "168384"
 "461330"
 "0"

```

```julia
split(replace(line1, r"(\pL) (\pL)"=>s"\1\u00a0\2","'"=>"")," ")
17-element Vector{SubString{String}}:
 "#1"
 "1"
 "0"
 "THIS TEXT"
 ""
 "88218681"
 "0.398"
 "1"
 ""
 "1"
 "1"
 "2"
 "0"
 "168384"
 "461330"
 "0"
 ""
# a more general sring

line4="#1 1 0 'THIS2 3TEXT' '' '88g 218x y681' 0.398 1 '' 1 1 2 0 168384 461330 0 "
split(replace(line4, r"(\w*\pL\w*) +"=>s"\1\u00a0","'"=>"")," ")
17-element Vector{SubString{String}}:
 "#1"
 "1"
 "0"
 "THIS2 3TEXT"
 ""
 "88g 218x y681"
 "0.398"
 "1"
 ""
 "1"
 "1"
 "2"
 "0"
 "168384"
 "461330"
 "0"
 ""

```

PS  
Can anyone explain why split(str, " ") and split(str) produce different results if there are multiple consecutive spaces (\x20)?

---

<div class="post-metadata">

**Author:** ![neophytedave](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neophytedave/32/202273_2.png) [@neophytedave](https://discourse.julialang.org/u/neophytedave)\
**Post date:** [May 15, 2023, 8:32pm UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/10 "2023-05-15T20:32:46Z")

</div>

I (think) I am working with a comparable situation. In my case, I copy Output from another application to the system clipboard. This causes the clipboard contents as seen by julia as a string. I then (painfully) interrogate each character of the clipboard to determine whether the character is part of the string you wish, e.g., “isequal(clipboard()[i],‘char’)”. too simple?

---

<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:** [May 16, 2023, 8:42am UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/11 "2023-05-16T08:42:28Z")

</div>

> [@uwbanjoman](#):
>
> how can i keep ‘THIS TEXT’ intact when splitting following string `"#1 1 0 'THIS TEXT' '' '88218681' 0.398 1 '' 1 1 2 0 168384 461330 0 "` at every “” ?

Regexes are an obvious first-choice solution for such problems. You specify what to find in the string, and voila:

```julia
julia> map(m -> strip(m.match, '\''), eachmatch(r"'[^']*'|\S+", str))
16-element Vector{SubString{String}}:
 "#1"
 "1"
 "0"
 "THIS TEXT"
 ""
 "88218681"
 "0.398"
 "1"
 ""
 "1"
 "1"
 "2"
 "0"
 "168384"
 "461330"
 "0"

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [May 16, 2023, 9:48am UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/12 "2023-05-16T09:48:49Z")

</div>

Another option (nesting of `'` will wreak havoc).

```julia
julia> line1 = "#1 1 0 'THIS TEXT' '' '88218681' 0.398 1 '' 1 1 2 0 168384 461330 0 "

julia> Iterators.flatmap((i,x)->isodd(i) ? split(x) : [x],
  Iterators.countfrom(1), split(line1, "'")) |> collect
16-element Vector{SubString{String}}:
 "#1"
 "1"
 "0"
 "THIS TEXT"
 ""
 "88218681"
 "0.398"
 "1"
 ""
 "1"
 "1"
 "2"
 "0"
 "168384"
 "461330"
 "0"

```

---

<div class="post-metadata">

**Author:** ![uwbanjoman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwbanjoman/32/9998_2.png) [@uwbanjoman](https://discourse.julialang.org/u/uwbanjoman)\
**Post date:** [May 16, 2023, 12:13pm UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/13 "2023-05-16T12:13:58Z")

</div>

Thank you all for your interest in my problem, learning a lot here.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 16, 2023, 1:43pm UTC](https://discourse.julialang.org/t/read-single-quoted-values-inside-strings/98877/14 "2023-05-16T13:43:27Z")

</div>

do you means this?  
but perhaps it is not a situation that can occur

```julia

line8="#1 1 'nest'THIS1 TEXT1'tsen' '' '88g 218x y681' '0.398 1 '' 1 1 2 0 168384 461330 0 "

Iterators.flatmap((i,x)->isodd(i) ? split(x) : [x],
  Iterators.countfrom(1), split(line8, "'")) |> collect

10-element Vector{SubString{String}}:
 "#1"
 "1"
 "nest"
 "THIS1"
 "TEXT1"
 "tsen"
 ""
 "88g 218x y681"
 "0.398 1 "
 " 1 1 2 0 168384 461330 0 "

map(m -> strip(m.match, '\''), eachmatch(r"'[^']*'|\S+", line8))
16-element Vector{SubString{String}}:
 "#1"
 "1"
 "nest"
 "THIS1"
 "TEXT1'tsen"
 ""
 "88g 218x y681"
 "0.398 1 "
 ""
 "1"
 "1"
 "2"
 "0"
 "168384"
 "461330"
 "0"

tokenize(line8)
8-element Vector{Any}:
 "#1"
 "1"
 "nest"
 "THIS1"
 "TEXT1tsen"
 ""
 "88g 218x y681"
 "0.398 1 "

split(replace(line8, r"(\w*\pL\w*) +"=>s"\1\u00a0","'"=>"")," ")
17-element Vector{SubString{String}}:
 "#1"
 "1"
 "nestTHIS1 TEXT1tsen"
 ""
 ""
 "88g 218x y681"
 "0.398"
 "1"
 ""
 "1"
 "1"
 "2"
 "0"
 "168384"
 "461330"
 "0"
 ""

```
