Skip to content

Commit d1adc17

Browse files
committed
Document that for "usual" regex behavior multiline is required
Regular expression users typically expect that matching a `$` in a multiline string would match the end of current line and not the end of the string past many lines. This is default behavior in pretty much every regexp engine: `grep`, `perl`, text editors, you name it… So it is fair to expect such expectation, so warn a user about necessity to pass `multiline` Fixes: #231
1 parent c38e6ea commit d1adc17

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/Parsing/String.purs

+26-11
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,11 @@ match p = do
202202
-- | error message.
203203
-- |
204204
-- | The returned parser will try to match the regular expression pattern once,
205-
-- | starting at the current parser position. On success, it will return
206-
-- | the matched substring.
205+
-- | starting at the current parser position. Note that this implies that an
206+
-- | expression starting as `^…` (i.e. with the beginning of line) will match the
207+
-- | current position even in absence of a preceding newline.
208+
-- |
209+
-- | On success, the parser will return the matched substring.
207210
-- |
208211
-- | If the RegExp `String` is constant then we can assume that compilation will
209212
-- | always succeed and `unsafeCrashWith` if it doesn’t. If we dynamically
@@ -231,14 +234,24 @@ match p = do
231234
-- |
232235
-- | #### Example
233236
-- |
234-
-- | This example shows how to compile and run the `xMany` parser which will
235-
-- | capture the regular expression pattern `x*`.
237+
-- | Compiling and running different regex parsers:
236238
-- |
237239
-- | ```purescript
238-
-- | case regex "x*" noFlags of
239-
-- | Left compileError -> unsafeCrashWith $ "xMany failed to compile: " <> compileError
240-
-- | Right xMany -> runParser "xxxZ" do
241-
-- | xMany
240+
-- | example re flags text =
241+
-- | case regex re flags of
242+
-- | Left compileError -> unsafeCrashWith $ "xMany failed to compile: " <> compileError
243+
-- | Right xMany -> runParser text do
244+
-- | xMany
245+
-- |
246+
-- | -- Capturing a string per `x*` regex.
247+
-- | exampleXMany = example "x*" noFlags "xxxZ"
248+
-- |
249+
-- | -- Capturing everything till end of line.
250+
-- | exampleCharsTillEol = example ".*$" multiline "line1\nline2"
251+
-- |
252+
-- | -- Capturing everything till end of string. Note the distinction with
253+
-- | -- `exampleCharsTillEol`.
254+
-- | exampleCharsTillEos = example ".*$" dotAll "line1\nline2"
242255
-- | ```
243256
-- |
244257
-- | #### Flags
@@ -249,9 +262,11 @@ match p = do
249262
-- | regex "x*" (dotAll <> ignoreCase)
250263
-- | ```
251264
-- |
252-
-- | The `dotAll`, `unicode`, and `ignoreCase` flags might make sense for
253-
-- | a `regex` parser. The other flags will
254-
-- | probably cause surprising behavior and you should avoid them.
265+
-- | The `dotAll`, `multiline`, `unicode`, and `ignoreCase` flags might make
266+
-- | sense for a `regex` parser. In fact, per JS RegExp semantics matching a
267+
-- | single line boundary in a multiline string requires passing `multiline`.
268+
-- |
269+
-- | Other flags will probably cause surprising behavior and should be avoided.
255270
-- |
256271
-- | [*MDN Advanced searching with flags*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#advanced_searching_with_flags)
257272
regex :: forall m. String -> RegexFlags -> Either String (ParserT String m String)

0 commit comments

Comments
 (0)