# What is regex for the end of string in Julia?

**URL:** <https://discourse.julialang.org/t/what-is-regex-for-the-end-of-string-in-julia/102228>\
**Category:** General Usage\
**Tags:** strings, regex\
**Created:** [July 29, 2023, 12:01pm UTC](https://discourse.julialang.org/t/what-is-regex-for-the-end-of-string-in-julia/102228 "2023-07-29T12:01:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 29, 2023, 12:01pm UTC](https://discourse.julialang.org/t/what-is-regex-for-the-end-of-string-in-julia/102228/1 "2023-07-29T12:01:30Z")

</div>

What is regular expression for the final end of string in Julia?

> **[Regular expression](https://en.wikipedia.org/wiki/Regular_expression)**
>
> A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a match pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. Regular expression techniques are developed in theoretical computer science and formal language theory.
> The concept of regular expressions began in the 1950s, when the American mathematician ...

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 29, 2023, 1:28pm UTC](https://discourse.julialang.org/t/what-is-regex-for-the-end-of-string-in-julia/102228/2 "2023-07-29T13:28:15Z")

</div>

It’s `$`, just like in all standard regex.

```julia
julia> x = "abc1 abc2 abc3";

julia> m = match(r"[a-z]*[1-9]$", x)
RegexMatch("abc3")

```

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 29, 2023, 3:12pm UTC](https://discourse.julialang.org/t/what-is-regex-for-the-end-of-string-in-julia/102228/3 "2023-07-29T15:12:32Z")

</div>

If you don’t know what is at the end of string then how will you find end ?

```julia
julia> x=" Raman Kumar"
" Raman Kumar"

julia> m = match(r"[a-z]*[1-9]$", x)

julia> 

```

---

<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:** [July 29, 2023, 3:16pm UTC](https://discourse.julialang.org/t/what-is-regex-for-the-end-of-string-in-julia/102228/4 "2023-07-29T15:16:49Z")

</div>

> [@pdeffebach](#):
>
> It’s `$`, just like in all standard regex.

Technically, `$` matches the end of a string not including a trailing newline.

```julia
julia> contains("foobar", r"r$")
true

julia> contains("foobar\n", r"r$") # ignores the trailing newline
true

julia> contains("foobar ", r"r$") # but doesn't ignore other whitespace
false

```

To match only the end, _not_ ignoring a newline, [use](https://perldoc.perl.org/perlre#Assertions) `\z`:

```julia
julia> contains("foobar", r"r\z")
true

julia> contains("foobar\n", r"r\z")
false

```

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 29, 2023, 3:25pm UTC](https://discourse.julialang.org/t/what-is-regex-for-the-end-of-string-in-julia/102228/5 "2023-07-29T15:25:38Z")

</div>

If i don’t know what is at end of string then what should i use ? r".$"

```julia
julia> x=" Raman Kumar yadav"
" Raman Kumar yadav"

julia> m =match(r"r$",x)

julia> m =match(r"v$",x)
RegexMatch("v")

julia> m =match(r".$",x)
RegexMatch("v")

julia> m =match(r".{5}$",x)
RegexMatch("yadav")

julia> 

```

---

<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:** [July 29, 2023, 3:27pm UTC](https://discourse.julialang.org/t/what-is-regex-for-the-end-of-string-in-julia/102228/6 "2023-07-29T15:27:23Z")

</div>

> [@raman\_kumar](#):
>
> If you don’t know what is at the end of string then how will you find end ?
> 
> ```julia
> julia> x=" Raman Kumar"
> " Raman Kumar"
> 
> julia> m = match(r"[a-z]*[1-9]$", x)
> 
> ```

This regular expression does not match your string `x` because `r"[a-z]*[1-9]$"` only matches a string that ends with a number `1-9`. That’s why it returns `m === nothing`.

If you just want to _know_ the last character of the string, don’t use a regex at all — you can just use `x[end]`:

```julia
julia> x[end]
'r': ASCII/Unicode U+0072 (category Ll: Letter, lowercase)

```

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 29, 2023, 3:38pm UTC](https://discourse.julialang.org/t/what-is-regex-for-the-end-of-string-in-julia/102228/7 "2023-07-29T15:38:59Z")

</div>

r".$" Will that be good regular expression for final end of the string ?

```julia
 r".$"

```

---

<div class="post-metadata">

**Author:** ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)\
**Post date:** [July 29, 2023, 4:29pm UTC](https://discourse.julialang.org/t/what-is-regex-for-the-end-of-string-in-julia/102228/8 "2023-07-29T16:29:33Z")

</div>

regular expression is for finding pattern, if there is a pattern only happens at the of a string _that you know_, you would use regex to find, and to match.

if you want to find the final end “character” of the string, you index it like mentioned.  
if you want to find the final end “token” (separated by white space in most language), you can use this `r"\s(\w+)$"`, or just `last(split(x))`
