Skip to content

Commit 576223b

Browse files
committed
Update readme with ref to t-regx and an exhaustive list of limitations, refs #5
1 parent fcf270a commit 576223b

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ composer/pcre
33

44
PCRE wrapping library that offers type-safe `preg_*` replacements.
55

6-
This library gives you a way to ensure `preg_*` functions do not fail silently, returning unexpected `null`s that may not be handled.
6+
This library gives you a way to ensure `preg_*` functions do not fail silently, returning
7+
unexpected `null`s that may not be handled.
78

8-
It also makes it easier ot work with static analysis tools like PHPStan or Psalm as it simplifies and reduces the possible return values from all the `preg_*` functions which are quite packed with edge cases.
9+
It also makes it easier ot work with static analysis tools like PHPStan or Psalm as it
10+
simplifies and reduces the possible return values from all the `preg_*` functions which
11+
are quite packed with edge cases.
12+
13+
This library is a thin wrapper around `preg_*` functions with [some limitations](#restrictions-limitations).
14+
If you are looking for a richer API to handle regular expressions have a look at
15+
[rawr/t-regx](https://packagist.org/packages/rawr/t-regx) instead.
916

1017
[![Continuous Integration](https://github.com/composer/pcre/workflows/Continuous%20Integration/badge.svg?branch=main)](https://github.com/composer/pcre/actions)
1118

@@ -61,9 +68,6 @@ The main difference is if anything fails to match/replace/.., it will throw a `C
6168
instead of returning `null` (or false in some cases), so you can now use the return values safely relying on
6269
the fact that they can only be strings (for replace), ints (for match) or arrays (for grep/split).
6370

64-
Similarly, due to type safety requirements matching using PREG_OFFSET_CAPTURE is made available via
65-
`matchWithOffset` and `matchAllWithOffset`. You cannot pass the flag to `match`/`matchAll`.
66-
6771
Additionally the `Preg` class provides match methods that return `bool` rather than `int`, for stricter type safety
6872
when the number of pattern matches is not useful:
6973

@@ -103,6 +107,26 @@ complex return types warranting a specific result object.
103107
See the [MatchResult](src/MatchResult.php), [MatchWithOffsetResult](src/MatchWithOffsetResult.php), [MatchAllResult](src/MatchAllResult.php),
104108
[MatchAllWithOffsetResult](src/MatchAllWithOffsetResult.php), and [ReplaceResult](src/ReplaceResult.php) class sources for more details.
105109

110+
Restrictions / Limitations
111+
--------------------------
112+
113+
Due to type safety requirements a few restrictions are in place.matchWithOffset
114+
115+
- matching using `PREG_OFFSET_CAPTURE` is made available via `matchWithOffset` and `matchAllWithOffset`.
116+
You cannot pass the flag to `match`/`matchAll`.
117+
- `Preg::split` will also reject `PREG_SPLIT_OFFSET_CAPTURE` and you should use `splitWithOffset`
118+
instead.
119+
- `matchAll` rejects `PREG_SET_ORDER` as it also changes the shape of the returned matches. There
120+
is no alternative provided as you can fairly easily code around it.
121+
- `preg_filter` is not supported as it has a rather crazy API, most likely you should rather
122+
use `Preg::grep` in combination with some loop and `Preg::replace`.
123+
- `replace`, `replaceCallback` and `replaceCallbackArray` do not support an array `$subject`,
124+
only simple strings.
125+
- in 2.x, we plan to always implicitly use `PREG_UNMATCHED_AS_NULL` for matching, which offers much
126+
saner/predictable results. This is currently not doable due to the PHP version requirement and to
127+
keep things working the same across all PHP versions. If you use the library on a PHP 7.2+ project
128+
however we highly recommend using `PREG_UNMATCHED_AS_NULL` with all `match*` and `replaceCallback*`
129+
methods.
106130

107131
License
108132
-------

0 commit comments

Comments
 (0)