# Learning Regular Expressions in Julia

**URL:** https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742
**Category:** Teaching & Outreach
**Tags:** question, documentation, regex
**Created:** [July 18, 2023, 4:56pm UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742 "2023-07-18T16:56:35Z")
**Posts on this page:** 14
**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 18, 2023, 4:56pm UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/1 "2023-07-18T16:56:35Z")

</div>

- I found it difficult to learn [Regular Expressions](https://docs.julialang.org/en/v1/manual/strings/#man-regex-literals) in Julia.

- I couldn’t understand what those different symbols in `r"^\s*(?:#|$)" ` stands for ? [Regular Expressions](https://docs.julialang.org/en/v1/manual/strings/#man-regex-literals) documentation is not detailed enough for many people to understand.

- Can you suggest some lucid references to understand Regular Expressions 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: ![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: [July 18, 2023, 5:22pm UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/2 "2023-07-18T17:22:37Z")

</div>

The regular expression syntax is not unique to any particular language, there are “dialects” of it to be sure, but they are all very similar. Some places I have found that discuss them:

- Python’s [re](https://docs.python.org/3/library/re.html) documentation.
- Racket’s [re](https://docs.racket-lang.org/guide/regexp.html) documentation.
- Vim [re](https://vimregex.com/) tutorial - note that vim has some strange dialect things going on with regular expressions.

The pattern you referenced in your question, I believe means:

- `^`: pattern starts with
- `\s*`: zero or more string characters (as many as possible, or “greedy”)
- `(?:`: is a non-capturing version of regular parenthesis in a regular expression. A _regular_ parenthesis captures each group so that it can be later referenced. This particular statement looks for either a `#` or a `$`.

---

<div class="post-metadata">

### Author: ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)
#### Post date: [July 18, 2023, 5:38pm UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/3 "2023-07-18T17:38:53Z")

</div>

Check out [Regex101](https://regex101.com/). I find it extremely helpful both in learning and prototyping regex.

---

<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: [July 18, 2023, 5:41pm UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/4 "2023-07-18T17:41:18Z")

</div>

You mention _prototyping_ regular expressions: this is more important than one might think. A poorly-crafted regular expression can eat up all of your memory. Imagine a webserver reading user input …

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [July 18, 2023, 6:12pm UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/5 "2023-07-18T18:12:41Z")

</div>

> [@raman\_kumar](#):
>
> Can you suggest some lucid references to understand Regular Expressions

[http://regex.info/book.html](http://regex.info/book.html)

---

<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 19, 2023, 10:52am UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/6 "2023-07-19T10:52:52Z")

</div>

Thank You , But this book is lengthy. 😆

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [July 19, 2023, 11:04am UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/7 "2023-07-19T11:04:18Z")

</div>

I highly recommend reading the first chapter. The author is very skilful at making a dry subject extremely fluid.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [July 19, 2023, 1:10pm UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/8 "2023-07-19T13:10:51Z")

</div>

You could also have a look at [GitHub - jkrumbiegel/ReadableRegex.jl: regexes for people who don't really want to learn or read regexes](https://github.com/jkrumbiegel/ReadableRegex.jl) and at the outputs of the different subfunctions to see what regexes they correspond to. Although the drawback is that it uses groups very liberally, so the output has a lot of unnecessary parentheses added (the computer doesn’t mind)

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 19, 2023, 1:18pm UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/9 "2023-07-19T13:18:53Z")

</div>

A little bit off-topic - and I also do not intend to set you off.

There is value in learning regex if you plan to do much work where you will use that. And if you really use a lot of regex - you’ll learn it anyway (by doing).

Otherwise, I suggest learning the basic principles and _search_ for close-enough examples when you need to create your own. Stackoverflow is overflowing with such examples.

Spending days acquiring a skill that you will rarely use can be a waste of time (mainly because regex is not like riding a bike - stuff will slip from your memory).

But if you really want to embark on such an adventure, the others suggested pretty lovely materials.

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [July 19, 2023, 2:17pm UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/10 "2023-07-19T14:17:58Z")

</div>

You can do a lot with just text, _wildcard_ `.`, _unlimited times_ `*`, _or_ `[]`, and _maybe_ `?`.  
I suggest you start with those and look up more complicated examples as needed.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/b/fbf4838e923a2e0bf7c63cd467bd69bf9f3ff081.png)

---

<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: [September 9, 2023, 3:41am UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/11 "2023-09-09T03:41:24Z")

</div>

**Mastering Regular Expressions** is excellent book. It helped me understand basics but it covers many different softwares for regex.  
Can you suggest some other more advanced book on regular expression compatible in JULIA ? :juliatext:

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [September 9, 2023, 4:37am UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/12 "2023-09-09T04:37:06Z")

</div>

> [@raman\_kumar](#):
>
> **Mastering Regular Expressions** is excellent book.

Actually, Friedl is known for doing tremendous damage to regexp with that book of his. One is sure to be left with strong misunderstandings after reading it, due to Friedl not understanding the relevant theory himself. I’d advise anyone not to read that book, because it’s prone to do more harm than good.

As an example of how Friedl butchers finite automata theory, he not only thinks that DFA isn’t equivalent to NFA, he actually doesn’t at all understand what NFA _is_, equating it to his own regexp implementation.

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [September 9, 2023, 9:45pm UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/13 "2023-09-09T21:45:41Z")

</div>

I love my regex joining function for building complex regex from simpler regex

```julia

#= function regex_and
     Join two regex together. ie. regex_A followed by regex_B
=#
function regex_and(a::Regex,b::Regex)
    a_str = string(a)
    b_str = string(b)
    result_str = "(?:" * a_str * ")" *
                 "(?:" * b_str * ")"
    return Regex(result_str)
end

#= function regex_or
     Select either one of the two regex. ie. regex_A or regex_B
=#
function regex_or(a::Regex,b::Regex)
    a_str = string(a)
    b_str = string(b)
    result_str = "(?:" *
                 "(?:" * a_str * ")" *
                 "|" *
                 "(?:" * b_str * ")" *
                 ")"
    return Regex(result_str)
end

#= function regex_or
     Select only one element of the array of regex.
=#
function regex_or(arr::Array{Regex,1})
    flag = false
    result_str = "(?:"
    for ele in arr
        if flag == true
            result_str = result_str * "|"
        else
            flag = true
        end
        temp_str = string(ele)
        result_str = result_str * "(?:" * temp_str * ")"
    end
    result_str = result_str * ")"
    return Regex(result_str)
end

```

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [September 10, 2023, 1:27pm UTC](https://discourse.julialang.org/t/learning-regular-expressions-in-julia/101742/14 "2023-09-10T13:27:52Z")

</div>

In addition to the other resources in this thread, I found [https://regexr.com/](https://regexr.com/) quite helpful to understand regexes I didn’t write.
