# Possible to parse strings containing strings?

**URL:** <https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081>\
**Category:** General Usage\
**Created:** [September 17, 2018, 4:28pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081 "2018-09-17T16:28:11Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [September 17, 2018, 4:28pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081/1 "2018-09-17T16:28:11Z")

</div>

I’m looking into calling functions with an input argument which is a string – normally of type:

```nohighlight
s = "K = 5"
myfunction(s)

```

where `myfunction` should parse the string argument and then set `K` equal to the number `5` when calling some `*.exe` file.

However, sometimes I’d like to set `K` equal to a _string_. Thus, `K = <some string>`, where `myfunction` should parse the string and set `K` equal to the string…

How can this be done?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 17, 2018, 6:41pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081/2 "2018-09-17T18:41:09Z")

</div>

> [@BLI](#):
>
> when calling some `*.exe` file.

Hm, what do you mean when calling “some `.exe` file”?

Anyway, it seems you just want to escape the quotes, e.g. `"K = \"string\""`?

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [September 17, 2018, 7:05pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081/3 "2018-09-17T19:05:08Z")

</div>

Maybe you already know, but just in case:

```julia
run(`some.exe`)

```

Note the command ticks inside run.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [September 17, 2018, 7:34pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081/4 "2018-09-17T19:34:19Z")

</div>

OK… let me try to be clearer… `myfunction` will call another program where some label K is set to either a number (float) or a string. I want to transfer this information (label, value) in a string. For a float, it is simple, e.g., `myfunction("K = 5")` and the argument can be parsed. But what if the value is a string?

It looks like your suggestion is `myfunction("K = \"some string\"")`. Is it simple to parse this and pick out `\"some string\"`, and then convert this to a string?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 17, 2018, 7:36pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081/5 "2018-09-17T19:36:09Z")

</div>

```julia
"K = \"some string\""

```

is the string with the content `K = "some string"` so should be fairly easy to parse that.

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [September 17, 2018, 8:04pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081/6 "2018-09-17T20:04:09Z")

</div>

Maybe you are looking something like:

```julia
julia> function myfun(;kwargs...)
           for arg in kwargs
               println("keyword = ",arg[1]," value = ",arg[2])
           end
       end
myfun (generic function with 2 methods)

julia> myfun(K=1,KK="test text",x="Another String")
keyword = K value = 1
keyword = KK value = test text
keyword = x value = Another String

julia> myfun(K=1)
keyword = K value = 1

julia> myfun(K="Some text")
keyword = K value = Some text

julia> 

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [September 17, 2018, 8:46pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081/7 "2018-09-17T20:46:59Z")

</div>

Not really. I’m looking form something like call `myfun("K=5")` should internally split the argument into `["K", 5]`, while `myfun("K=\"some string\"")` should internally split the argument into `["K", "some string"]`.

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [September 17, 2018, 8:55pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081/8 "2018-09-17T20:55:13Z")

</div>

That is exactly what my code demonstrates. `arg` in my code is your `["K",1]` in yours.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [September 17, 2018, 8:59pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081/9 "2018-09-17T20:59:20Z")

</div>

Perhaps a clarifying comment… the reason why `myfun(K=5)` or `myfun(K="some string")` won’t do, is that my example label `K` in reality can be an identifier (for another language) that is not a valid Julia identifier. This other language allows identifiers which could be `"a.b"`, `"d(T)"`, `"a.d(T)"`, `"a.b.d(T)"`, etc. And the assigned value may be a number or a string. Anyway, I assume I can figure out how to parse `"K=5"` vs. `"K=\"some string\""`…

As far as I know, `myfun(a.b.d(T)=5)` or `myfun(a.b.d(T)="string")` won’t work.

The label names come from this other language, so I am not at liberty to limit the identifiers to be valid Julia identifiers.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 17, 2018, 9:06pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081/10 "2018-09-17T21:06:32Z")

</div>

Just use `split`.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [September 17, 2018, 9:30pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081/11 "2018-09-17T21:30:36Z")

</div>

Yes… I tried with the following:

```nohighlight
function myparse(arg)
   splt_arg = split(arg,"=")
   spl2 = try parse(Float64,splt_arg[2])
   catch
       split(splt_arg[2],"\"")[2]
    end
    return [splt_arg[1], spl2]
end

```

Test:

```nohighlight
julia> stra = "K=5"
julia> strb = "a.d(T) = \"test string\""
julia> myparse(stra)
2-element Array{Any,1}:
  "K"
 5.0
julia> myparse(strb)
2-element Array{SubString{String},1}:
 "a.d(T) "
 "test string"

```

Probably not very elegant code… and I should trim off some spaces.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 17, 2018, 10:03pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081/12 "2018-09-17T22:03:09Z")

</div>

Why do you need to parse things into julia objects to then send them over to another program? And if you need to do that, what subset of julia do you need to support on the RHS of the assignment?

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [September 20, 2018, 7:06pm UTC](https://discourse.julialang.org/t/possible-to-parse-strings-containing-strings/15081/13 "2018-09-20T19:06:50Z")

</div>

Sorry for not coming back to your comment. Turned out we could just split the string at the equality sign, and then convert to float if the string contained a number, and keep as string otherwise.
