Skip to content

Commit

Permalink
Corrected errors in chp05
Browse files Browse the repository at this point in the history
  • Loading branch information
lafith committed Nov 4, 2020
1 parent b03b7a2 commit ab2a311
Showing 1 changed file with 19 additions and 39 deletions.
58 changes: 19 additions & 39 deletions _chapters/08-ex5.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,39 +244,39 @@ If you are familiar with regular expressions, plod ahead! However, if

looks like gobbledygook to you or you feel your regex fu is a little rusty, put down this book and consult the [Regex cheatsheet](http://www.rexegg.com/regex-quickstart.html#ref) or, even better, [Jeffrey Friedl's amazing book on mastering regexes](http://regex.info/book.html).

### Finding and replacing using the `search()` function
### Finding Substring

If you are only concerned with finding a single instance of a search term within a string, the `search()` function returns the range index of where the search expression appears:
If you are only concerned with finding a single instance of a search term within a string, the `findfirst()` function returns the range index of where the search expression appears:

```julia
julia> search(declaration, "Government")
julia> findfirst("Government",declaration)
241:250

```

`search()` also accepts regular expressions:
`findfirst()` also accepts regular expressions:

```julia
julia> search(declaration, r"th.{2,3}")
julia> findfirst(r"th.{2,3}", declaration)
9:13

```

To retrieve the result, rather than its index, you can pass the resulting index off to the string as the subsetting range, using the square bracket `[]` syntax:

```julia
julia> declaration[search(declaration, r"th.{2,3}")]
julia> declaration[findfirst(r"th.{2,3}", declaration)]
"these"

```

Ah, so that's the word it found!

Where a search string is not found, `search()` will yield `0:-1`. That is an odd result, until you realise the reason: for any string `s`, `s[0:-1]` will necessarily yield `""` (that is, an empty string).
Where a search string is not found, `findfirst()` will yield `nothing`.

### Finding using the `match()` family of functions

The problem with `search()` is that it retrieves one, and only one, result – the first within the string passed to it. The `match()` family of functions can help us with finding more results:
The problem with `findfirst()` is that it retrieves one, and only one, result – the first within the string passed to it. The `match()` family of functions can help us with finding more results:

- `match()` retrieves _either the first match or nothing_ within the text,
- `matchall()` returns _an array of all matching substrings_, and
Expand All @@ -299,7 +299,7 @@ is valid, while
yields an error:

```julia
ERROR: `match` has no method matching match(::String, ::String)
ERROR: MethodError: no method matching match(::String, ::String)

```

Expand Down Expand Up @@ -329,7 +329,7 @@ To illustrate, let's consider the result of a `match()` call, which will be intr

#### `match()`

`match()` retrieves the first match or nothing - in this sense, it is rather similar to `search()`:
`match()` retrieves the first match or nothing - in this sense, it is rather similar to `findfirst()`:

```julia
julia> match(r"That .*?,", declaration)
Expand All @@ -339,18 +339,6 @@ To illustrate, let's consider the result of a `match()` call, which will be intr

The result is a `RegexMatch` object. The object can be inspected using `.match` (e.g. `match(r"truths", declaration).match`).

#### `matchall()`

`matchall()` returns an array of matching substrings, which is sometimes a little easier to use:

```julia
julia> matchall(r"That .*?,", declaration)
2-element Array{SubString{UTF8String},1}:
"That to secure these rights,"
"That whenever any Form of Government becomes destructive of these ends,"

```

#### `eachmatch()`

`eachmatch()` returns an object known as an iterator, specifically of the type `RegexMatchIterator`. We have on and off encountered iterators, but we will not really deal with them in depth until chapter [X], which deals with control flow. Suffice it to say an iterator is an object that contains a list of items that can be iterated through. The iterator will iterate over a list of `RegexMatch` objects, so if we want the results themselves, we will need to call the `.match` method on each of them:
Expand All @@ -361,24 +349,23 @@ The result is a `RegexMatch` object. The object can be inspected using `.match`
end

```

The result is quite similar to that returned by `matchall()`:
gives following result:

```julia
A matching search result is: That to secure these rights,
A matching search result is: That whenever any Form of Government becomes destructive of these ends,

```

#### `ismatch()`
#### `occursin()`

`ismatch()` returns a boolean value depending on whether the search text contains a match for the regex provided.
`occursin()` returns a boolean value depending on whether the search text contains a match for the regex provided.

```julia
julia> ismatch(r"truth(s)?", declaration)
julia> occursin(r"truth(s)?", declaration)
true

julia> ismatch(r"sausage(s)?", declaration)
julia> occursin(r"sausage(s)?", declaration)
false

```
Expand All @@ -388,18 +375,11 @@ The result is quite similar to that returned by `matchall()`:
Julia can replace substrings using the `replace()` syntax... let's try putting some sausages into the Declaration of Independence!

```julia
julia> replace(declaration, "truth", "sausage")
julia> replace(declaration, "truth"=>"sausage",count=1)
"We hold these sausages to be self-evident, that all men are created equal,..."

```

An interesting feature of `replace()` is that the replacement does not need to be a string. In fact, it is possible to pass a function as the third argument (as always, without the parentheses `()` that signify a function call). Julia will interpret this as 'replace the substring with the result of passing the substring to this function':

```julia
julia> replace(declaration, "truth", uppercase)
"We hold these TRUTHs to be self-evident, that all men are created equal,..."

```
By increasing the value of count you can replace remaining "truth".

Much more dignified than self-evident sausages, I'd say! At risk of repeating myself, it is important to note that since strings are immutable, `replace()` merely returns a copy of the string with the search string replaced by the replacement string or the result of the replacement function, and the original string itself will remain unaffected.

Expand Down Expand Up @@ -435,8 +415,8 @@ Case transformations are functions that act on `String`s and transform character
|:--------:|--------|--------|
| `uppercase()` | Converts the entire string to upper-case characters | `WE HOLD THESE TRUTHS TO BE SELF-EVIDENT` |
| `lowercase()` | Converts the entire string to lower-case characters | `we hold these truths to be self-evident` |
| `ucfirst()` | Converts the first character of the string to upper-case | `We hold these truths to be self-evident` |
| `lcfirst()` | Converts the first character of the string ot lower-case | `we hold these truths to be self-evident` |
| `uppercasefirst()` | Converts the first character of the string to upper-case | `We hold these truths to be self-evident` |
| `lowercasefirst()` | Converts the first character of the string ot lower-case | `we hold these truths to be self-evident` |

### Testing and attributes

Expand Down

0 comments on commit ab2a311

Please sign in to comment.