Skip to content

Commit

Permalink
doc: add example that uses an alternation
Browse files Browse the repository at this point in the history
And we make it an interesting example, i.e., one that demonstrates
preference order semantics.

Closes #610
  • Loading branch information
BurntSushi committed Mar 15, 2023
1 parent b3f8e00 commit 67be79a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,25 @@ xy concatenation (x followed by y)
x|y alternation (x or y, prefer x)
</pre>
This example shows how an alternation works, and what it means to prefer a
branch in the alternation over subsequent branches.
```
use regex::Regex;
let haystack = "samwise";
// If 'samwise' comes first in our alternation, then it is
// preferred as a match, even if the regex engine could
// technically detect that 'sam' led to a match earlier.
let re = Regex::new(r"samwise|sam").unwrap();
assert_eq!("samwise", re.find(haystack).unwrap().as_str());
// But if 'sam' comes first, then it will match instead.
// In this case, it is impossible for 'samwise' to match
// because 'sam' is a prefix of it.
let re = Regex::new(r"sam|samwise").unwrap();
assert_eq!("sam", re.find(haystack).unwrap().as_str());
```
## Repetitions
<pre class="rust">
Expand Down

0 comments on commit 67be79a

Please sign in to comment.