<div dir="ltr">Note that Haskell doesn&#39;t convert &quot;\(&quot; to &quot;\\(&quot; like Python does, so the regex string has been changed. It&#39;s not so easy to figure out how to use this library from the documentation because of all the typeclass trickery.<div>
<br></div><div style>h&gt; import Text.Regexp.Posix (getAllTextSubmatches, (=~))</div><div><div>h&gt; getAllTextSubmatches (&quot;M(1,2) = 0.1e-3;&quot; =~ &quot;M\\(([0-9]+),([0-9]+)\\) *= *([0-9.eE-]+);&quot;) :: [String]</div>
<div>[&quot;M(1,2) = 0.1e-3;&quot;,&quot;1&quot;,&quot;2&quot;,&quot;0.1e-3&quot;]</div><div><br></div><div style>If it doesn&#39;t match, you&#39;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 ().</div>
<div style><br></div><div style>To demonstrate some of the typeclass trickery going on here, you could ask for the results as an Array with Int indexes of String.</div><div style><br></div><div style><div>h&gt; import Text.Regexp.Posix (getAllTextSubmatches, (=~))</div>
<div style>h&gt; import Data.Array ((!), Array)</div><div><div><div>h&gt; let matches = getAllTextSubmatches (&quot;M(1,2) = 0.1e-3;&quot; =~ &quot;M\\(([0-9]+),([0-9]+)\\) *= *([0-9.eE-]+);&quot;) :: Array Int String</div>
<div>h&gt; matches</div><div>array (0,3) [(0,&quot;M(1,2) = 0.1e-3;&quot;),(1,&quot;1&quot;),(2,&quot;2&quot;),(3,&quot;0.1e-3&quot;)]</div><div>h&gt; map (matches !) [1..3]</div><div>[&quot;1&quot;,&quot;2&quot;,&quot;0.1e-3&quot;]</div>
</div></div></div><div style><br></div><div class="gmail_extra"><div style>-bob</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 18, 2013 at 3:25 PM, Nicolas Bock <span dir="ltr">&lt;<a href="mailto:nicolasbock@gmail.com" target="_blank">nicolasbock@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">Hello list,<div><br></div><div>I was wondering whether there is a way to do what the following python code does in haskell:</div>
<div><br></div><div><div>import re</div><div><br></div><div>

result = re.compile(&quot;M\(([0-9]+),([0-9]+)\) *= *([0-9.eE-]+);&quot;).search(&quot;M(1,2) = 0.1e-3;&quot;)</div><div>if result:</div><div>  print(result.group(1), result.group(2), result.group(3))</div><div><br></div>


<div>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&#39;s re module.</div>


<div><br></div><div>Thanks already,</div><div><br></div><div>nick</div><div><br></div></div></div>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div></div></div>