You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Early Introduction to Quantifiers & Combining Terms
43
43
44
44
The DSL supports common regex quantifiers as methods on every `Term`. These methods allow you to specify how many times a pattern should be matched. They include:
45
45
46
-
-**`times(count)`**: Matches the term exactly `count` times.
46
+
-**`exactly(count)`**: Matches the term exactly `count` times.
47
47
-**`optional()`**: Matches the term zero or one time.
48
48
-**`one_or_more()`**: Matches the term one or more times (Kleene Plus).
49
49
-**`zero_or_more()`**: Matches the term zero or more times (Kleene Star).
50
-
-**`repeat(min_count, max_count)`**: Matches the term between `min_count` and `max_count` times (or open-ended if one value is omitted).
50
+
-**`between(min_count, max_count)`**: Matches the term between `min_count` and `max_count` times (inclusive).
51
+
-**`at_least(count)`**: Matches the term at least `count` times.
52
+
-**`at_most(count)`**: Matches the term up to `count` times.
51
53
52
-
Let’s see these quantifiers side by side with examples.
54
+
These quantifiers can also be used as functions that take the `Term` as an argument. If the term is a plain string, it will be automatically converted to a `String` object. Thus `String("foo").optional()` is equivalent to `optional("foo")`.
55
+
56
+
Let's see these quantifiers side by side with examples.
53
57
54
58
### Quantifiers in Action
55
59
56
-
#### `times(count)`
60
+
#### `exactly(count)`
57
61
58
62
This method restricts the term to appear exactly `count` times.
59
63
60
64
```python
61
65
# Example: exactly 5 digits
62
-
five_digits = Regex(r"\d").times(5)
66
+
five_digits = Regex(r"\d").exactly(5)
63
67
print(to_regex(five_digits)) # Output: (\d){5}
64
68
```
65
69
66
-
You can also use the `times` function:
70
+
You can also use the `exactly` function:
67
71
68
72
```python
69
-
from outlines.types importtimes
73
+
from outlines.types importexactly
70
74
71
75
# Example: exactly 5 digits
72
-
five_digits =times(Regex(r"\d"), 5)
76
+
five_digits =exactly(Regex(r"\d"), 5)
73
77
print(to_regex(five_digits)) # Output: (\d){5}
74
78
```
75
79
76
80
#### `optional()`
77
81
78
-
The `optional()` method makes a term optional, meaning it may occur zero or one time.
82
+
This method makes a term optional, meaning it may occur zero or one time.
79
83
80
84
```python
81
85
# Example: an optional "s" at the end of a word
82
86
maybe_s = String("s").optional()
83
87
print(to_regex(maybe_s)) # Output: (s)?
84
88
```
85
89
86
-
You can also use the `optional` function, the string will automatically be converted to a `String` object:
The `|` operator creates alternatives, allowing a match for one of several patterns.
219
+
The `either()` function creates alternatives, allowing a match for one of several patterns. You can provide as many terms as you want.
192
220
193
221
```python
194
-
# Example: Match either "cat" or "dog"
195
-
animal = String("cat")|"dog"
196
-
print(to_regex(animal)) # Output: (cat|dog)
222
+
# Example: Match either "cat" or "dog" or "mouse"
223
+
animal =either(String("cat"), "dog", "mouse")
224
+
print(to_regex(animal)) # Output: (cat|dog|mouse)
197
225
```
198
226
199
-
*Note:* When using operators with plain strings (such as `"dog"`), the DSL automatically wraps them in a `String` object and escapes the characters that have a special meaning in regular expressions.
227
+
*Note:* When using `either()` with plain strings (such as `"dog"`), the DSL automatically wraps them in a `String` object that escapes the characters that have a special meaning in regular expressions, just like with quantifier functions.
200
228
201
229
---
202
230
@@ -223,7 +251,7 @@ For instance you can describe the answers in the GSM8K dataset using the followi
223
251
```python
224
252
from outlines.types import sentence, digit
225
253
226
-
answer ="A: "+ sentence.repeat(2,4) +" So the answer is: "+ digit.repeat(1,4)
254
+
answer ="A: "+ sentence.between(2,4) +" So the answer is: "+ digit.between(1,4)
227
255
```
228
256
229
257
---
@@ -237,7 +265,7 @@ Suppose you want to create a regex that matches an ID format like "ID-12345", wh
237
265
- Followed by exactly 5 digits.
238
266
239
267
```python
240
-
id_pattern ="ID-"+ Regex(r"\d").times(5)
268
+
id_pattern ="ID-"+ Regex(r"\d").exactly(5)
241
269
print(to_regex(id_pattern)) # Output: ID-(\d){5}
242
270
```
243
271
@@ -273,9 +301,9 @@ When used in a Pydantic model, the email field is automatically validated agains
273
301
Consider a pattern to match a simple date format: `YYYY-MM-DD`.
274
302
275
303
```python
276
-
year = Regex(r"\d").times(4) # Four digits for the year
277
-
month = Regex(r"\d").times(2)# Two digits for the month
278
-
day = Regex(r"\d").times(2)# Two digits for the day
304
+
year = Regex(r"\d").exactly(4) # Four digits for the year
305
+
month = Regex(r"\d").exactly(2) # Two digits for the month
306
+
day = Regex(r"\d").exactly(2) # Two digits for the day
0 commit comments