Nicolas Bock | 19 Jan 00:25 2013
Picon

regular expression sub-expression matching

Hello list,

I was wondering whether there is a way to do what the following python code does in haskell:

import re

result = re.compile("M\(([0-9]+),([0-9]+)\) *= *([0-9.eE-]+);").search("M(1,2) = 0.1e-3;")
if result:
  print(result.group(1), result.group(2), result.group(3))

Basically I would like to pattern match parts of a string and return the matches. I have looked at Text.Regex.Posix but (of course I might just not have understood the functions in there properly) it seems as if it does not provide anything like python's re module.

Thanks already,

nick

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Roman Cheplyaka | 19 Jan 00:47 2013

Re: regular expression sub-expression matching

* Nicolas Bock <nicolasbock <at> gmail.com> [2013-01-18 16:25:31-0700]
> Hello list,
> 
> I was wondering whether there is a way to do what the following python code
> does in haskell:
> 
> import re
> 
> result = re.compile("M\(([0-9]+),([0-9]+)\) *=
> *([0-9.eE-]+);").search("M(1,2) = 0.1e-3;")
> if result:
>   print(result.group(1), result.group(2), result.group(3))
> 
> Basically I would like to pattern match parts of a string and return the
> matches. I have looked at Text.Regex.Posix but (of course I might just not
> have understood the functions in there properly) it seems as if it does not
> provide anything like python's re module.

Take a look at the regex-applicative package.

http://hackage.haskell.org/packages/archive/regex-applicative/0.2.1/doc/html/Text-Regex-Applicative.html

Roman
Bob Ippolito | 19 Jan 01:52 2013

Re: regular expression sub-expression matching

Note that Haskell doesn't convert "\(" to "\\(" like Python does, so the regex string has been changed. It's not so easy to figure out how to use this library from the documentation because of all the typeclass trickery.

h> import Text.Regexp.Posix (getAllTextSubmatches, (=~))
h> getAllTextSubmatches ("M(1,2) = 0.1e-3;" =~ "M\\(([0-9]+),([0-9]+)\\) *= *([0-9.eE-]+);") :: [String]
["M(1,2) = 0.1e-3;","1","2","0.1e-3"]

If it doesn't match, you'll get an empty list. You can join the tail of the match list together with Data.List.intercalate, and print it out with putStrLn. The if null case would just return ().

To demonstrate some of the typeclass trickery going on here, you could ask for the results as an Array with Int indexes of String.

h> import Text.Regexp.Posix (getAllTextSubmatches, (=~))
h> import Data.Array ((!), Array)
h> let matches = getAllTextSubmatches ("M(1,2) = 0.1e-3;" =~ "M\\(([0-9]+),([0-9]+)\\) *= *([0-9.eE-]+);") :: Array Int String
h> matches
array (0,3) [(0,"M(1,2) = 0.1e-3;"),(1,"1"),(2,"2"),(3,"0.1e-3")]
h> map (matches !) [1..3]
["1","2","0.1e-3"]

-bob

On Fri, Jan 18, 2013 at 3:25 PM, Nicolas Bock <nicolasbock <at> gmail.com> wrote:
Hello list,

I was wondering whether there is a way to do what the following python code does in haskell:

import re

result = re.compile("M\(([0-9]+),([0-9]+)\) *= *([0-9.eE-]+);").search("M(1,2) = 0.1e-3;")
if result:
  print(result.group(1), result.group(2), result.group(3))

Basically I would like to pattern match parts of a string and return the matches. I have looked at Text.Regex.Posix but (of course I might just not have understood the functions in there properly) it seems as if it does not provide anything like python's re module.

Thanks already,

nick


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Nicolas Bock | 22 Jan 00:21 2013
Picon

Re: regular expression sub-expression matching

Hi Roman and Bob,

thanks a lot for the help. I ended up using getAllTextSubmatches as explained by Bob, since I had that package already installed on my system. Works great!

Thanks,

nick



On Fri, Jan 18, 2013 at 5:52 PM, Bob Ippolito <bob <at> redivi.com> wrote:
Note that Haskell doesn't convert "\(" to "\\(" like Python does, so the regex string has been changed. It's not so easy to figure out how to use this library from the documentation because of all the typeclass trickery.

h> import Text.Regexp.Posix (getAllTextSubmatches, (=~))
h> getAllTextSubmatches ("M(1,2) = 0.1e-3;" =~ "M\\(([0-9]+),([0-9]+)\\) *= *([0-9.eE-]+);") :: [String]
["M(1,2) = 0.1e-3;","1","2","0.1e-3"]

If it doesn't match, you'll get an empty list. You can join the tail of the match list together with Data.List.intercalate, and print it out with putStrLn. The if null case would just return ().

To demonstrate some of the typeclass trickery going on here, you could ask for the results as an Array with Int indexes of String.

h> import Text.Regexp.Posix (getAllTextSubmatches, (=~))
h> import Data.Array ((!), Array)
h> let matches = getAllTextSubmatches ("M(1,2) = 0.1e-3;" =~ "M\\(([0-9]+),([0-9]+)\\) *= *([0-9.eE-]+);") :: Array Int String
h> matches
array (0,3) [(0,"M(1,2) = 0.1e-3;"),(1,"1"),(2,"2"),(3,"0.1e-3")]
h> map (matches !) [1..3]
["1","2","0.1e-3"]

-bob

On Fri, Jan 18, 2013 at 3:25 PM, Nicolas Bock <nicolasbock <at> gmail.com> wrote:
Hello list,

I was wondering whether there is a way to do what the following python code does in haskell:

import re

result = re.compile("M\(([0-9]+),([0-9]+)\) *= *([0-9.eE-]+);").search("M(1,2) = 0.1e-3;")
if result:
  print(result.group(1), result.group(2), result.group(3))

Basically I would like to pattern match parts of a string and return the matches. I have looked at Text.Regex.Posix but (of course I might just not have understood the functions in there properly) it seems as if it does not provide anything like python's re module.

Thanks already,

nick


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane