title: Haskell extract substring within a string
body: <p>My goal is to find the number of times a substring exists within a string.
  The substring I'm looking for will be of type "[n]", where n can be any variable.</p>
  
  <p>My attempt involved splitting the string up using the words function,
  then create a new list of strings if the 'head' of a string was '[' and
  the 'last' of the same string was ']'</p>
  
  <p>The problem I ran into was that I entered a String which when split using 
  the function words, created a String that looked like this "[2],"
  Now, I still want this to count as an occurrence of the type "[n]"</p>
  
  <p>An example would be I would want this String, </p>
  
  <p>asdf[1]jkl[2]asdf[1]jkl</p>
  
  <p>to return 3.</p>
  
  <p>Here's the code I have:</p>
  
  <pre><code>-- String that will be tested on references function
  txt :: String
  txt = "[1] and [2] both feature characters who will do whatever it takes to " ++
    "get to their goal, and in the end the thing they want the most ends " ++
    "up destroying them.  In case of [2], this is a whale..."
  
  -- Function that will take a list of Strings and return a list that contains
  -- any String of the type [n], where n is an variable
  ref :: [String] -&gt; [String]
  ref [] = []
  ref xs = [x | x &lt;- xs, head x == '[', last x == ']']
  
  -- Function takes a text with references in the format [n] and returns
  -- the total number of references.
  -- Example :  ghci&gt; references txt -- -&gt; 3
  references :: String -&gt; Integer   
  references txt = len (ref (words txt))
  </code></pre>
  
  <p>If anyone can enlighten me on how to search for a substring within a string
  or how to parse a string given a substring, that would be greatly appreciated.</p>