title: Clearer way to parse a token out of a string in ruby
body: <p>I'm trying to clean up something for the hell of it, and looking for some better ways of going about it. The idea is that instead of using regular expressions in my rules for parsing a string, I'd like to use something closer to the routes syntax "something/:searchitem/somethingelse", then given a string like "/something/FOUNDIT/somethingelse" you'd get the result "FOUNDIT".</p>
  
  <p>Here's the example I'm refactoring:
  Given an input string, say "<a href="http://claimid.com/myusername" rel="nofollow noreferrer">http://claimid.com/myusername</a>".  I want to be able to run this string against a number of possible matches and then return the "myusername" one for the one that matches.</p>
  
  <p>The data to run it against might look like this:</p>
  
  <pre><code>PROVIDERS = [
    "http://openid.aol.com/:username",
    "http://:username.myopenid.com",
    "http://claimid.com/:username",
    "http://:username.livejournal.com"]
  
    something_here("http://claimid.com/myusername") # =&gt; "myusername" 
  </code></pre>
  
  <p>Any good way of matching up a string like <code>http://claimid.com/myusername</code> to this list and making sense of the results? Or any techniques to make something like this easier? I was looking through the rails routing code as it does something like this, but that's not the easiest code to follow.</p>
  
  <hr>
  
  <p>Right now I'm just doing this with regular expressions, but it seems like the above method would be MUCH easier to read</p>
  
  <pre><code>PROVIDERS = [
    /http:\/\/openid.aol.com\/(\w+)/,
    /http:\/\/(\w+).myopenid.com/,
    /http:\/\/(\w+).livejournal.com/,
    /http:\/\/flickr.com\/photos\/(\w+)/,
    /http:\/\/technorati.com\/people\/technorati\/(\w+)/,
    /http:\/\/(\w+).wordpress.com/,
    /http:\/\/(\w+).blogspot.com/,
    /http:\/\/(\w+).pip.verisignlabs.com/,
    /http:\/\/(\w+).myvidoop.com/,
    /http:\/\/(\w+).pip.verisignlabs.com/,
    /http:\/\/claimid.com\/(\w+)/]
  
  url = "http://claimid.com/myusername"
  username = PROVIDERS.collect { |provider|
    url[provider, 1]
  }.compact.first
  </code></pre>