Skip to content

Commit

Permalink
Update the Regex section of strings.md. (JuliaLang#29921)
Browse files Browse the repository at this point in the history
* Update the Regex section of strings.md.

This adds a brief description and example of using the Regex constructor to create regex strings programmatically.

* Update doc/src/manual/strings.md

Co-Authored-By: lewisl <[email protected]>

* Update doc/src/manual/strings.md

Co-Authored-By: lewisl <[email protected]>

* Update doc/src/manual/strings.md

Co-Authored-By: lewisl <[email protected]>
  • Loading branch information
lewisl authored and KristofferC committed Nov 19, 2018
1 parent be98372 commit a1aa542
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions doc/src/manual/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,33 @@ ERROR: syntax: invalid escape sequence
Triple-quoted regex strings, of the form `r"""..."""`, are also supported (and may be convenient
for regular expressions containing quotation marks or newlines).

The `Regex()` constructor may be used to create a valid regex string programmatically. This permits using the contents of string variables and other string operations when constructing the regex string. Any of the regex codes above can be used within the single string argument to `Regex()`. Here are some examples:

```jldoctest
julia> using Dates
julia> d = Date(1962,7,10)
1962-07-10
julia> regex_d = Regex("Day " * string(day(d)))
r"Day 10"
julia> match(regex_d, "It happened on Day 10")
RegexMatch("Day 10")
julia> name = "Jon"
"Jon"
julia> regex_name = Regex("[\"( ]$name[\") ]") # interpolate value of name
r"[\"( ]Jon[\") ]"
julia> match(regex_name," Jon ")
RegexMatch(" Jon ")
julia> match(regex_name,"[Jon]") === nothing
true
```

## [Byte Array Literals](@id man-byte-array-literals)

Another useful non-standard string literal is the byte-array string literal: `b"..."`. This
Expand Down

0 comments on commit a1aa542

Please sign in to comment.