• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

manoss96 / pregex / 4356417821

pending completion
4356417821

push

github

manoss96
add logo and funding

1625 of 1625 relevant lines covered (100.0%)

3.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/pregex/core/assertions.py
1
__doc__ = """
3✔
2
All classes within this module "assert" something about the provided pattern
3
without having to match any additional characters. For example, :class:`MatchAtStart`
4
ensures that the provided pattern matches only when it is found at the start of the string,
5
while :class:`NotFollowedBy` asserts that a match must not be followed by one or more
6
specified patterns. Another thing you should keep in mind is that many of these assertions
7
cannot be repeated, as attempting that will cause a ``CannotBeRepeatedException`` exception
8
to be thrown.
9

10
Classes & methods
11
-------------------------------------------
12

13
Below are listed all classes within :py:mod:`pregex.core.assertions`
14
along with any possible methods they may possess.
15
"""
16

17

18
import pregex.core.pre as _pre
3✔
19
import pregex.core.exceptions as _ex
3✔
20
from typing import Union as _Union
3✔
21

22

23
class __Assertion(_pre.Pregex):
3✔
24
    '''
3✔
25
    Constitutes the base class for `__Anchor` and `__Lookaround` classes.
26

27
    :param str pattern: The RegEx pattern which represents the assertion.
28
    '''
29
    def __init__(self, pattern: str):
3✔
30
        '''
31
        Constitutes the base class for `__Anchor` and `__Lookaround` classes.
32

33
        :param str pattern: The RegEx pattern which represents the assertion.
34
        '''
35
        super().__init__(pattern, escape=False)
3✔
36

37

38
class __Anchor(__Assertion):
3✔
39
    '''
3✔
40
    Constitutes the base class for all `anchor` classes that are part of this module.
41

42
    :param Pregex | str pre: A Pregex instance or string representing the `anchor` pattern.
43
    :param (Pregex => str) transform: A `transform` function for the provided pattern.
44

45
    :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a ``Pregex`` instance \
46
        nor a string.
47
    '''
48
    def __init__(self, pre: _Union[_pre.Pregex, str], transform):
3✔
49
        '''
50
        Constitutes the base class for all `anchor` classes that are part of this module.
51

52
        :param Pregex | str pre: A Pregex instance or string representing the `anchor` pattern.
53
        :param (Pregex => str) transform: A `transform` function for the provided pattern.
54

55
        :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a ``Pregex`` instance \
56
            nor a string.
57
        '''
58
        super().__init__(str(transform(__class__._to_pregex(pre))))
3✔
59

60

61
class __Lookaround(__Assertion):
3✔
62
    '''
3✔
63
    Constitutes the base class for all "Lookaround" classes.
64

65
    :param Pregex | str pres: Two or more Pregex instances, the first of which always \
66
        represents the `match` pattern, while the rest constitute `assertion` patterns.
67
    :param (tuple[Pregex | str] => str) transform: A `transform` function for the provided patterns.
68

69
    :raises NotEnoughArgumentsException: No assertion patterns were provided.
70
    :raises EmptyNegativeAssertionException: The empty string is provided \
71
        as one of the assertion patterns.
72
    '''
73
    def __init__(self, pres: tuple[_Union[_pre.Pregex, str]], transform) -> _pre.Pregex:
3✔
74
        '''
75
        Constitutes the base class for all "Lookaround" classes.
76

77
        :param Pregex | str pres: Two or more Pregex instances, the first of which always \
78
            represents the `match` pattern, while the rest constitute `assertion` patterns.
79
        :param (tuple[Pregex | str] => str) transform: A `transform` function for the provided patterns.
80

81
        :raises NotEnoughArgumentsException: No assertion patterns were provided.
82
        :raises EmptyNegativeAssertionException: The empty string is provided \
83
            as one of the assertion patterns.
84
        '''
85
        if len(pres) < 2:
3✔
86
            message = "At least one assertion pattern is required."
3✔
87
            raise _ex.NotEnoughArgumentsException(message)
3✔
88
        result = __class__._to_pregex(pres[0])
3✔
89
        for pre in pres[1:]:
3✔
90
            result = transform(result, pre)
3✔
91
        super().__init__(str(result))
3✔
92

93

94
class MatchAtStart(__Anchor):
3✔
95
    '''
3✔
96
    Matches the provided pattern only if it is at the start of the string.
97

98
    :param Pregex | str pre: The pattern that is to be matched.
99

100
    :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
101
        ``Pregex`` instance nor a string.
102

103
    :note: The resulting pattern cannot have a repeating quantifier applied to it.
104
    '''
105

106
    def __init__(self, pre: _Union[_pre.Pregex, str]):
3✔
107
        '''
108
        Matches the provided pattern only if it is at the start of the string.
109

110
        :param Pregex | str pre: The pattern that is to be matched.
111

112
        :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
113
            ``Pregex`` instance nor a string.
114

115
        :note: The resulting pattern cannot have a repeating quantifier applied to it.
116
        '''
117
        super().__init__(pre, lambda pre: pre.match_at_start())
3✔
118

119

120
class MatchAtEnd(__Anchor):
3✔
121
    '''
3✔
122
    Matches the provided pattern only if it is at the end of the string.
123

124
    :param Pregex | str pre: The pattern that is to be matched.
125

126
    :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
127
        ``Pregex`` instance nor a string.
128

129
    :note: The resulting pattern cannot have a repeating quantifier applied to it.
130
    '''
131

132
    def __init__(self, pre: _Union[_pre.Pregex, str]):
3✔
133
        '''
134
        Matches the provided pattern only if it is at the end of the string.
135

136
        :param Pregex | str pre: The pattern that is to be matched.
137

138
        :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
139
            ``Pregex`` instance nor a string.
140

141
        :note: The resulting pattern cannot have a repeating quantifier applied to it.
142
        '''
143
        super().__init__(pre, lambda pre: pre.match_at_end())
3✔
144

145

146
class MatchAtLineStart(__Anchor):
3✔
147
    '''
3✔
148
    Matches the provided pattern only if it is at the start of a line.
149

150
    :param Pregex | str pre: The pattern that is to be matched.
151

152
    :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
153
        ``Pregex`` instance nor a string.
154

155
    :note:
156
        - The resulting pattern cannot have a repeating quantifier applied to it.
157
        - Uses meta character ``^`` since the `MULTILINE` flag is considered on.
158
    '''
159

160
    def __init__(self, pre: _Union[_pre.Pregex, str]):
3✔
161
        '''
162
        Matches the provided pattern only if it is at the start of a line.
163

164
        :param Pregex | str pre: The pattern that is to be matched.
165

166
        :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
167
            ``Pregex`` instance nor a string.
168

169
        :note:
170
            - The resulting pattern cannot have a repeating quantifier applied to it.
171
            - Uses meta character ``^`` since the `MULTILINE` flag is considered on.
172
        '''
173
        super().__init__(pre, lambda pre: pre.match_at_line_start())
3✔
174

175

176
class MatchAtLineEnd(__Anchor):
3✔
177
    '''
3✔
178
    Matches the provided pattern only if it is at the end of a line.
179

180
    :param Pregex | str pre: The pattern that is to be matched.
181

182
    :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
183
        ``Pregex`` instance nor a string.
184

185
    :note:
186
        - The resulting pattern cannot have a repeating quantifier applied to it.
187
        - Uses meta character ``$`` since the `MULTILINE` flag is considered on.
188
    '''
189

190
    def __init__(self, pre: _Union[_pre.Pregex, str]):
3✔
191
        '''
192
        Matches the provided pattern only if it is at the end of a line.
193

194
        :param Pregex | str pre: The pattern that is to be matched.
195

196
        :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
197
            ``Pregex`` instance nor a string.
198

199
        :note:
200
            - The resulting pattern cannot have a repeating quantifier applied to it.
201
            - Uses meta character ``$`` since the `MULTILINE` flag is considered on.
202
        '''
203
        super().__init__(pre, lambda pre: pre.match_at_line_end())
3✔
204

205

206
class WordBoundary(__Anchor):
3✔
207
    '''
3✔
208
    Asserts that the position, at which an instance of this class is placed, \
209
    must constitute a word boundary.
210
    '''
211

212
    def __init__(self):
3✔
213
        '''
214
        Asserts that the position, at which an instance of this class is placed, \
215
        must constitute a word boundary.
216
        '''
217
        super().__init__(_pre.Pregex(), lambda pre: pre.concat(_pre.Pregex("\\b", escape=False)))
3✔
218

219

220
class NonWordBoundary(__Anchor):
3✔
221
    '''
3✔
222
    Asserts that the position, at which an instance of this class is placed, \
223
    must not constitute a word boundary.
224
    '''
225

226
    def __init__(self):
3✔
227
        '''
228
        Asserts that the position, at which an instance of this class is placed, \
229
        must not constitute a word boundary.
230
        '''
231
        super().__init__(_pre.Pregex(), lambda pre: pre.concat(_pre.Pregex("\\B", escape=False)))
3✔
232

233

234
class FollowedBy(__Lookaround):
3✔
235
    '''
3✔
236
    Matches pattern ``match`` only if it is directly followed \
237
    by all of the provided ``assertion`` patterns.
238

239
    :param Pregex | str match: A Pregex instance or string \
240
        representing the `match` pattern.
241
    :param Pregex | str \*assertions: One or more patterns, all of which must \
242
        come right after pattern ``match`` in order for it to be considered a match.
243

244
    :raises NotEnoughArgumentsException: No assertion patterns were provided.
245
    :raises InvalidArgumentTypeException: At least one of the provided arguments \
246
        is neither a ``Pregex`` instance nor a string.
247

248
    :note: The resulting pattern cannot have a repeating quantifier applied to it.
249
    '''
250

251
    def __init__(self, match: _Union[_pre.Pregex, str], *assertions: _Union[_pre.Pregex, str]):
3✔
252
        '''
253
        Matches pattern ``match`` only if it is directly followed \
254
        by all of the provided ``assertion`` patterns.
255

256
        :param Pregex | str match: A Pregex instance or string \
257
            representing the `match` pattern.
258
        :param Pregex | str \*assertions: One or more patterns, all of which must \
259
            come right after pattern ``match`` in order for it to be considered a match.
260

261
        :raises NotEnoughArgumentsException: No assertion patterns were provided.
262
        :raises InvalidArgumentTypeException: At least one of the provided arguments \
263
            is neither a ``Pregex`` instance nor a string.
264

265
        :note: The resulting pattern cannot have a repeating quantifier applied to it.
266
        '''
267
        super().__init__((match, *assertions), lambda pre1, pre2: pre1.followed_by(pre2))
3✔
268

269

270
class PrecededBy(__Lookaround):
3✔
271
    '''
3✔
272
    Matches pattern ``match`` only if it is directly preceded \
273
    by all of the provided ``assertion`` patterns.
274

275
    :param Pregex | str match: A Pregex instance or string \
276
        representing the `match` pattern.
277
    :param Pregex | str \*assertions: One or more patterns, all of which must \
278
        come right before pattern ``match`` in order for it to be considered a match.
279

280
    :raises NotEnoughArgumentsException: No assertion patterns were provided.
281
    :raises InvalidArgumentTypeException: At least one of the provided arguments \
282
        is neither a ``Pregex`` instance nor a string.
283
    :raises NonFixedWidthPatternException: Parameter ``assertion`` \
284
        corresponds to a pattern that does not have a fixed width.
285

286
    :note: The resulting pattern cannot have a repeating quantifier applied to it.
287
    '''
288

289
    def __init__(self, match: _Union[_pre.Pregex, str], *assertions: _Union[_pre.Pregex, str]):
3✔
290
        '''
291
        Matches pattern ``match`` only if it is directly preceded \
292
        by all of the provided ``assertion`` patterns.
293

294
        :param Pregex | str match: A Pregex instance or string \
295
            representing the `match` pattern.
296
        :param Pregex | str \*assertions: One or more patterns, all of which must \
297
            come right before pattern ``match`` in order for it to be considered a match.
298

299
        :raises NotEnoughArgumentsException: No assertion patterns were provided.
300
        :raises InvalidArgumentTypeException: At least one of the provided arguments \
301
            is neither a ``Pregex`` instance nor a string.
302
        :raises NonFixedWidthPatternException: Parameter ``assertion`` \
303
            corresponds to a pattern that does not have a fixed width.
304

305
        :note: The resulting pattern cannot have a repeating quantifier applied to it.
306
        '''
307
        super().__init__((match, *assertions), lambda pre1, pre2: pre1.preceded_by(pre2))
3✔
308

309

310
class EnclosedBy(__Lookaround):
3✔
311
    '''
3✔
312
    Matches pattern ``match`` only if it is both directly preceded \
313
    and followed by all of the provided ``assertion`` patterns.
314

315
    :param Pregex | str match: A Pregex instance or string \
316
        representing the `match` pattern.
317
    :param Pregex | str \*assertions: One or more patterns, all of which must \
318
        come both right before and right after pattern ``match`` in order for \
319
        it to be considered a match.
320

321
    :raises NotEnoughArgumentsException: No assertion patterns were provided.
322
    :raises InvalidArgumentTypeException: At least one of the provided arguments \
323
        is neither a ``Pregex`` instance nor a string.
324
    :raises NonFixedWidthPatternException: Parameter ``assertion`` \
325
        corresponds to a pattern that does not have a fixed width.
326

327
    :note: The resulting pattern cannot have a repeating quantifier applied to it.
328
    '''
329

330
    def __init__(self, match: _Union[_pre.Pregex, str], *assertions: _Union[_pre.Pregex, str]):
3✔
331
        '''
332
        Matches pattern ``match`` only if it is both directly preceded \
333
        and followed by all of the provided ``assertion`` patterns.
334

335
        :param Pregex | str match: A Pregex instance or string \
336
            representing the `match` pattern.
337
        :param Pregex | str \*assertions: One or more patterns, all of which must \
338
            come both right before and right after pattern ``match`` in order for \
339
            it to be considered a match.
340

341
        :raises NotEnoughArgumentsException: No assertion patterns were provided.
342
        :raises InvalidArgumentTypeException: At least one of the provided arguments \
343
            is neither a ``Pregex`` instance nor a string.
344
        :raises NonFixedWidthPatternException: Parameter ``assertion`` \
345
            corresponds to a pattern that does not have a fixed width.
346

347
        :note: The resulting pattern cannot have a repeating quantifier applied to it.
348
        '''
349
        super().__init__((match, *assertions), lambda pre1, pre2: pre1.enclosed_by(pre2))
3✔
350

351

352
class NotFollowedBy(__Lookaround):
3✔
353
    '''
3✔
354
    Matches pattern ``match`` only if it is not directly followed by \
355
    any one of the provided ``assertions`` patterns.
356

357
    :param Pregex | str match: The pattern that is to be matched.
358
    :param Pregex | str \*assertions: One or more patterns, none of which must \
359
        come right after pattern ``match`` in order for it to be considered a match.
360

361
    :raises NotEnoughArgumentsException: No assertion patterns were provided.
362
    :raises InvalidArgumentTypeException: At least one of the provided arguments \
363
        is neither a ``Pregex`` instance nor a string.
364
    :raises EmptyNegativeAssertionException: At least one of the provided assertion \
365
        patterns is the empty-string pattern.
366
    '''
367

368
    def __init__(self, match: _Union[_pre.Pregex, str], *assertions: _Union[_pre.Pregex, str]):
3✔
369
        '''
370
        Matches pattern ``match`` only if it is not directly followed by \
371
        any one of the provided ``assertions`` patterns.
372

373
        :param Pregex | str match: The pattern that is to be matched.
374
        :param Pregex | str \*assertions: One or more patterns, none of which must \
375
            come right after pattern ``match`` in order for it to be considered a match.
376

377
        :raises NotEnoughArgumentsException: No assertion patterns were provided.
378
        :raises InvalidArgumentTypeException: At least one of the provided arguments \
379
            is neither a ``Pregex`` instance nor a string.
380
        :raises EmptyNegativeAssertionException: At least one of the provided assertion \
381
            patterns is the empty-string pattern.
382
        '''
383
        super().__init__((match, *assertions),
3✔
384
            lambda pre1, pre2: pre1.not_followed_by(pre2))
385

386

387
class NotPrecededBy(__Lookaround):
3✔
388
    '''
3✔
389
    Matches pattern ``match`` only if it is not directly preceded by \
390
    any one of the provided ``assertions`` patterns.
391

392
    :param Pregex | str match: The pattern that is to be matched.
393
    :param Pregex | str \*assertions: One or more patterns, none of which must \
394
        come right before pattern ``match`` in order for it to be considered a match.
395

396
    :raises NotEnoughArgumentsException: No assertion patterns were provided.
397
    :raises InvalidArgumentTypeException: At least one of the provided arguments \
398
        is neither a ``Pregex`` instance nor a string.
399
    :raises EmptyNegativeAssertionException: At least one of the provided assertion \
400
        patterns is the empty-string pattern.
401
    :raises NonFixedWidthPatternException: At least one of the provided assertion \
402
        patterns does not have a fixed width.
403
    '''
404

405
    def __init__(self, match: _Union[_pre.Pregex, str], *assertions: _Union[_pre.Pregex, str]):
3✔
406
        '''
407
        Matches pattern ``match`` only if it is not directly preceded by \
408
        any one of the provided ``assertions`` patterns.
409

410
        :param Pregex | str match: The pattern that is to be matched.
411
        :param Pregex | str \*assertions: One or more patterns, none of which must \
412
            come right before pattern ``match`` in order for it to be considered a match.
413

414
        :raises NotEnoughArgumentsException: No assertion patterns were provided.
415
        :raises InvalidArgumentTypeException: At least one of the provided arguments \
416
            is neither a ``Pregex`` instance nor a string.
417
        :raises EmptyNegativeAssertionException: At least one of the provided assertion \
418
            patterns is the empty-string pattern.
419
        :raises NonFixedWidthPatternException: At least one of the provided assertion \
420
            patterns does not have a fixed width.
421
        '''
422
        super().__init__((match, *assertions),
3✔
423
            lambda pre1, pre2: pre1.not_preceded_by(pre2))
424

425

426
class NotEnclosedBy(__Lookaround):
3✔
427
    '''
3✔
428
    Matches pattern ``match`` only if it is neither directly preceded \
429
    nor followed by any one of the provided ``assertions`` patterns.
430

431
    :param Pregex | str match: The pattern that is to be matched.
432
    :param Pregex | str \*assertions: One or more patterns, none of which must \
433
        come either right before or right after pattern ``match`` in order for \
434
        it to be considered a match.
435

436
    :raises NotEnoughArgumentsException: No assertion patterns were provided.
437
    :raises InvalidArgumentTypeException: At least one of the provided arguments \
438
        is neither a ``Pregex`` instance nor a string.
439
    :raises EmptyNegativeAssertionException: At least one of the provided assertion \
440
        patterns is the empty-string pattern.
441
    :raises NonFixedWidthPatternException: At least one of the provided assertion \
442
        patterns does not have a fixed width.
443
    '''
444

445
    def __init__(self, match: _Union[_pre.Pregex, str], *assertions: _Union[_pre.Pregex, str]):
3✔
446
        '''
447
        Matches pattern ``match`` only if it is neither directly preceded \
448
        nor followed by any one of the provided ``assertions`` patterns.
449

450
        :param Pregex | str match: The pattern that is to be matched.
451
        :param Pregex | str \*assertions: One or more patterns, none of which must \
452
            come either right before or right after pattern ``match`` in order for \
453
            it to be considered a match.
454

455
        :raises NotEnoughArgumentsException: No assertion patterns were provided.
456
        :raises InvalidArgumentTypeException: At least one of the provided arguments \
457
            is neither a ``Pregex`` instance nor a string.
458
        :raises EmptyNegativeAssertionException: At least one of the provided assertion \
459
            patterns is the empty-string pattern.
460
        :raises NonFixedWidthPatternException: At least one of the provided assertion \
461
            patterns does not have a fixed width.
462
        '''
463
        super().__init__((match, *assertions),
3✔
464
            lambda pre1, pre2: pre1.not_enclosed_by(pre2))
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc