Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help for Haskell regular expression

Tags:

regex

haskell

Given some strings: "\begin", "\end" or "\section" etc.

I want to match them with following function and regular expression:

matchTest (mkRegex '\\[:alnum:]+')  "\begin"

I always get False when I test it in GHCi,

Does anyone know how to match the pattern like "\begin" or "\end" etc.

like image 930
Aron Lee Avatar asked Apr 23 '26 00:04

Aron Lee


1 Answers

> matchTest (mkRegex "\\\\[a-zA-Z0-9]+") "\\begin"
True

There are a couple of issues with your line of code.

  • The regular expression isn't quoted correctly, the string needs to be within double quotes. (But you should get a syntax error for that.)
  • The regular expressions supported by regex-compat (I assume that's the package you're using) are the same as those supported by egrep, and they don't know :alnum:.
  • Backslash \ is an escape character both within Haskell strings and regular expressions, so you need to escape it.
like image 132
robx Avatar answered Apr 25 '26 17:04

robx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!