Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline option appears to be ignored #11

Open
raxod502 opened this issue May 12, 2020 · 2 comments
Open

Multiline option appears to be ignored #11

raxod502 opened this issue May 12, 2020 · 2 comments
Labels
documentation Improvements or additions to documentation PR welcome

Comments

@raxod502
Copy link

In GHCI:

Text.Regex.TDFA> "f" =~~ "." :: Maybe String
Just "f"
Text.Regex.TDFA> "\n" =~~ "." :: Maybe String
Nothing

The documentation says multiline matching should be enabled by default, but just to double-check, I tried enabling it explicitly:

Text.Regex.TDFA> (makeRegexOpts defaultCompOpt { multiline = True } defaultExecOpt "." :: Regex) `matchM` "f" :: Maybe String
Just "f"
Text.Regex.TDFA> (makeRegexOpts defaultCompOpt { multiline = True } defaultExecOpt "." :: Regex) `matchM` "\n" :: Maybe String
Nothing

No luck. The same thing happens for inverted character classes (e.g., [^&] instead of .). Am I misunderstanding how to get my regex to match newlines?

@obfusk
Copy link

obfusk commented Feb 19, 2021

regex-tdfa seems to have a non-standard multiline mode that combines what is usually known as "multiline" (i.e. having "^" and "$" match at the beginning/end of individual lines, not just the whole string) with inverse "dotall" and also disables matching newlines in inverted character classes (so you can't even use e.g. [^&] as you mentioned).

You can match a newline using "(.|\n)", but only with an actual newline in the pattern (since \n is just n to regex-tdfa).

> ("\n" =~~ "(.|\n)") :: Maybe String 
Just "\n"

It does recognise [[:space:]] though, so this works as well (in case your pattern is e.g. user input that can't contain a newline):

> ("\n" =~~ "(.|[[:space:]])") :: Maybe String 
Just "\n"

If you don't care about "^" and "$" but want "." to match newlines, use multiline = False:

> let p = makeRegexOpts defaultCompOpt { multiline = False } defaultExecOpt "."
> p `matchM` "\n" :: Maybe String 
Just "\n"

@raxod502
Copy link
Author

Thanks for the pointer. I think this issue then becomes a request for the documentation to include this helpful information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation PR welcome
Projects
None yet
Development

No branches or pull requests

3 participants