# Can I input an invalid string into a variable?

**URL:** <https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769>\
**Category:** General Usage\
**Tags:** strings\
**Created:** [March 30, 2022, 9:12pm UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769 "2022-03-30T21:12:17Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [March 30, 2022, 9:12pm UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/1 "2022-03-30T21:12:17Z")

</div>

Hi!

Suppose I have string such as:

```julia
Part_\d{4}

```

Putting it into a variable will give:

```julia
ERROR: syntax: invalid escape sequence

```

But if instead do:

`Variable = r"Part_\d{4}"`

Which works, which is great. But what if I want that to be a function input? So like something like:

```julia
function GoodIdea(strVar)
     return r strVar
end

```

Can I do this? Thanks.

Kind regards

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 30, 2022, 9:21pm UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/2 "2022-03-30T21:21:23Z")

</div>

> [@Ahmed\_Salih](#):
>
> `Part_\d{4}`

```julia
julia> a = "Part_\\d{4}";

julia> println(a)
Part_\d{4}

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 31, 2022, 1:28am UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/3 "2022-03-31T01:28:01Z")

</div>

See [escape sequences](https://en.wikipedia.org/wiki/Escape_sequences_in_C) … this is not specific to Julia. Every programming language with strings has some kind of escaping, usually very similar.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [March 31, 2022, 7:58pm UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/4 "2022-03-31T19:58:08Z")

</div>

I see, it is just nicer for a user of my program to not have to have knowledge about escape sequences and just input it as usual. Regex can get ugly pretty quickly if we have to escape everything.

This is why I hoped there was a function to do `r"Part_\d{4}"` but since I cannot hold the variable in a string it is meaningless unless someone can do macro tricks to solve it.

Kind regards

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [March 31, 2022, 8:04pm UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/5 "2022-03-31T20:04:13Z")

</div>

```julia
julia> var"Part_\d{4}" = -3
-3

julia> abs(var"Part_\d{4}")
3

```

```julia
help?> @var_str
search: @var_str

  var

  The syntax var"#example#" refers to a variable named Symbol("#example#"), even though #example# is not a valid Julia
  identifier name.

  This can be useful for interoperability with programming languages which have different rules for the construction
  of valid identifiers. For example, to refer to the R variable draw.segments, you can use var"draw.segments" in your
  Julia code.

  It is also used to show julia source code which has gone through macro hygiene or otherwise contains variable names
  which can't be parsed normally.

  Note that this syntax requires parser support so it is expanded directly by the parser rather than being implemented
  as a normal string macro @var_str.

  │ Julia 1.3
  │
  │ This syntax requires at least Julia 1.3.

```

\*\*edit: sorry, I misunderstood the question, this won’t address your issue

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 31, 2022, 8:19pm UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/6 "2022-03-31T20:19:44Z")

</div>

> [@Ahmed\_Salih](#):
>
> I see, it is just nicer for a user of my program to not have to have knowledge about escape sequences and just input it as usual. Regex can get ugly pretty quickly if we have to escape everything.

String macros help with this. I’m not entirely clear on why you can’t just use `r"..."` if you want regexes, but if you want an ordinary string containing a regex (or a portion thereof) you can just use [raw strings](https://docs.julialang.org/en/v1/manual/strings/#man-raw-string-literals), e.g. `raw"Part_\d{4}"`.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [March 31, 2022, 8:21pm UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/7 "2022-03-31T20:21:37Z")

</div>

But if I want an user to input “Part\_\d{4}” into a function, how would I allow him to do that?

Say I want to do something like this:

```julia
function BadString(varStr)
     return raw(varStr)
end

```

So that the user does not have to deal with '' etc. for all kind of symbols.

Kind regards

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 31, 2022, 8:22pm UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/8 "2022-03-31T20:22:28Z")

</div>

> [@Ahmed\_Salih](#):
>
> But if I want an user to input “Part\_\d{4}” into a function, how would I allow him to do that?

Tell the user to pass `raw"Part_\d{4}"`?

If the users are calling your function from Julia, then presumably they will have to learn at least _some_ Julia syntax.

It would be helpful to have a little more context to understand what you are trying to do.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [March 31, 2022, 8:24pm UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/9 "2022-03-31T20:24:50Z")

</div>

I think you actually just answered what I wanted to do 🙂 You are right in just informing the user about inputting `raw` infront of the string, I thought that I could do that step for them - which turns out is not possible easily.

Kind regards

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 31, 2022, 10:44pm UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/10 "2022-03-31T22:44:21Z")

</div>

If they’re inputting Julia expressions you could just have them give you a regex object which has the syntax you want. If they’re inputting raw text from a prompt or a file then you can implement whatever escaping rules you want.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 1, 2022, 6:06pm UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/11 "2022-04-01T18:06:53Z")

</div>

I think it is the second part of your statement I don’t know how to do. If an user provided me with:

```julia
Part_\d{4}

```

I cannot see how I would save this input into a variable etc. Do you mean by reading the data in from text file? The reason I struggle is, I know it is not possible to write:

```julia
var = "Part_\d{4}"

```

But I know it is possible to do:

```julia
var = raw"Part_\d{4}"

```

Maybe my understanding is flawed / I struggle to explain it, I guess I just want to be able to take a string I know has faulty syntax, save it in a variable and then perform “raw” on it.

Kind regards

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [April 1, 2022, 7:35pm UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/12 "2022-04-01T19:35:18Z")

</div>

You say you know about raw, so is this like the use case you expect:

```julia
julia> s=readline(stdin);
Part_\d{4}

julia> @show s
s = "Part_\\d{4}"
"Part_\\d{4}"

```

---

<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:** [April 1, 2022, 10:27pm UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/13 "2022-04-01T22:27:15Z")

</div>

> [@Ahmed\_Salih](#):
>
> I guess I just want to be able to take a string I know has faulty syntax, save it in a variable and then perform “raw” on it.

There is no way to “perform” `raw`, nor string Part\_\d{4} has invalid “syntax”.

The **string literal** `"Part_\d{4}"` has invalid syntax, this does not mean that Part\_\d{4} is an invalid value for a string to have, just that if you need to input this string value in code with a “normal” string literal then you need to type `"Part_\\d{4}"` instead. This string literal represents the string Part\_\d{4} which is entirely valid.

`raw"string"` is not something that is “applied”, it is just a way for you to say to the compiler: “oh, and this string literal here, use this another set of rules for parsing it into a string value, a set of rules in which `\` is _almost always_ interpreted as just `\`, and nothing more”. It does change the string value that you get from a string literal but just because now you are not, in fact, using a “normal” _string literal_ but a _raw string literal_ that follows other rules.

As @Jeff_Emanuel pointed out, if you do not need to type it in code, then this basically does not matter, the inputter can write anything without caring for those rules and in the end you will get exact what you put in. The only fault of Jeff’s example is that `show` uses the string literal notation to show the string, so it seems like it does have two backlashes but this is not the case:

```julia
julia> s=readline(stdin);
Part_\d{4}

julia> @show s
s = "Part_\\d{4}"
"Part_\\d{4}"

julia> println(s)
Part_\d{4}

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [April 2, 2022, 2:25am UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/14 "2022-04-02T02:25:49Z")

</div>

What I’m trying to get at is are they “inputting” this as Julia code or as some text that you read from somewhere (prompt, text file, etc.)?

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 2, 2022, 6:27am UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/15 "2022-04-02T06:27:48Z")

</div>

Oh, thanks for clarifying!

I would ideally like both. I have made a function which reads files based on a specific Regex:

```julia
##Lists files in directory and only returns applicable files, ie. "Part_XXXX.bi4"
function _dirFiles(rgxPat::Regex=Regex("Part_\\d{4}.bi4"))
    files = readdir()
    #Operation on dirFiles instantly
    filter!(x -> occursin(rgxPat, x), files)
    return files
end

```

(The reason path variable is not used in readdir, is because I “cd” into specific folder first)

I would like an user to be able to both use it from terminal, but without having to think about syntax i.e. writing in terminal or in a script file:

```julia
cd("some/path")
Files = _dirFiles("Part_\d{4}")

```

Without having to think about escaping, and being able to input a Regex string “directly”.

I know understand that I can just ask the user, “remember to put `raw`” infront - and that is probably what I would do at the end.

I was just curious if there was a way to go around this and do the `raw` for the user in the function.

Kind regards

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 2, 2022, 7:11am UTC](https://discourse.julialang.org/t/can-i-input-an-invalid-string-into-a-variable/78769/16 "2022-04-02T07:11:14Z")

</div>

I don’t think it’s a good idea to try to help your users like this. If you don’t require the user to specify whether the input is interpreted as a regular string, a “raw” string or a regex, how will you know what they intend? Maybe they _didn’t_ want regex search, but actually want to find the sequence of characters ‘Part\_\d{4}’.

If the users are writing Julia code, and are supposed to supply regexes, it is better to tell them to _actually_ create a regex (it is _really super easy_ and useful) rather than secretly turning their input into a regex behind their backs. By trying to make it easy for them, you might instead make things inscrutable and confusing, and even make some things impossible to do.
