# Using Regex() function, how do I match the end of the string?

**URL:** <https://discourse.julialang.org/t/using-regex-function-how-do-i-match-the-end-of-the-string/123659>\
**Category:** General Usage\
**Tags:** question, regex\
**Created:** [December 10, 2024, 12:58pm UTC](https://discourse.julialang.org/t/using-regex-function-how-do-i-match-the-end-of-the-string/123659 "2024-12-10T12:58:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![francisbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/francisbarton/32/205194_2.png) [@francisbarton](https://discourse.julialang.org/u/francisbarton)\
**Post date:** [December 10, 2024, 12:58pm UTC](https://discourse.julialang.org/t/using-regex-function-how-do-i-match-the-end-of-the-string/123659/1 "2024-12-10T12:58:48Z")

</div>

I am using the `Regex()` function to create a regular expression pattern, instead of just using `r""`, because I need to interpolate a variable.

If I write `Regex("$(f)$")` - interpolating the variable `f` and wanting to match where it is at the end of a string - I get the error

> identifier or parenthesized expression expected after $ in string

OK - but then how _do_ I represent the end of the string?

Searching the web for “Julia Regex end of string” just gives me various lectures on how to write regular expressions.

---

<div class="post-metadata">

**Author:** ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)\
**Post date:** [December 10, 2024, 1:04pm UTC](https://discourse.julialang.org/t/using-regex-function-how-do-i-match-the-end-of-the-string/123659/2 "2024-12-10T13:04:45Z")

</div>

The problem here is that the `$` is being evaluated as a normal Julia string, and so it signifies interpolation, which is not the case when using the `r""` macro.

The solution is to escape the last `$` so that it’s interpreted as a literal character `$`, _not_ an interpolation operator:

```julia
f = "an interpolated value"
Regex("$(f)\$")

```

---

<div class="post-metadata">

**Author:** ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)\
**Post date:** [December 10, 2024, 1:11pm UTC](https://discourse.julialang.org/t/using-regex-function-how-do-i-match-the-end-of-the-string/123659/3 "2024-12-10T13:11:27Z")

</div>

I don’t know your use case, but you might be able to use `*`, as Julia Regex values support concatenation with strings

```julia
julia> rx = "a normal string" * r"$"
r"\Qa normal string\E(?:$)"

julia> match(rx, "this is a normal string")
RegexMatch("a normal string")

julia> match(rx, "this is a non-matching string")

julia>

```

---

<div class="post-metadata">

**Author:** ![francisbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/francisbarton/32/205194_2.png) [@francisbarton](https://discourse.julialang.org/u/francisbarton)\
**Post date:** [December 10, 2024, 1:23pm UTC](https://discourse.julialang.org/t/using-regex-function-how-do-i-match-the-end-of-the-string/123659/4 "2024-12-10T13:23:32Z")

</div>

Thank you - I was avoiding doing that _because_ I thought that escaping it would mean it got interpreted as a literal $ character not as an end of string indicator!

---

<div class="post-metadata">

**Author:** ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)\
**Post date:** [December 10, 2024, 2:05pm UTC](https://discourse.julialang.org/t/using-regex-function-how-do-i-match-the-end-of-the-string/123659/5 "2024-12-10T14:05:22Z")

</div>

That makes sense! Escaping in Regex is already tedious, and combined with standard string handling it leaves a lot of room for confusion.

For the sake of clarity, here’s how the process works, with an example not using interpolation, `Regex("hello\$")`

1. the string within the expression `"hello\$"` is evaluated first, and results in the value `hello$` because of the escaped `$`
2. The value `hello$` is passed to the `Regex` constructor which processes it as a regular expression, which interprets the `$` as “end of string”

When you use `r""` you are _skipping_ step 1, and whatever you write in that string is passed directly to `Regex`, with no string “evaluation” step beforehand.

Hence, you can write a regular expression directly: `r"hello$"`
