title: grep on unix / linux: how to replace or capture text?
body: <p>So I'm pretty good with regular expressions, but I'm having some trouble with them on unix. Here are two things I'd love to know how to do:</p>
  
  <p>1) <strong>Replace all text except letters, numbers, and underscore</strong></p>
  
  <p>In PHP I'd do this: (works great)<pre>
  preg_replace('#[^a-zA-Z0-9_]#','',$text).
  </pre></p>
  
  <p>In bash I tried this (with limited success); seems like it dosen't allow you to use the full set of regex:  <pre>
  text="my #1 example!"
  ${text/[^a-zA-Z0-9_]/'')
  </pre></p>
  
  <p>I tried it with sed but it still seems to have problems with the full regex set:</p>
  
  <pre>echo "my #1 example!" | sed s/[^a-zA-Z0-9\_]//
  </pre>
  
  <p>I'm sure there is a way to do it with grep, too, but it was breaking it into multiple lines when i tried:</p>
  
  <pre>echo abc\!\@\#\$\%\^\&\*\(222 | grep -Eos '[a-zA-Z0-9\_]+'</pre>
  
  <p>And finally I also tried using <strong>expr</strong> but it seemed like that had <em>really</em> limited support for extended regex...
  <br><br>
  <hr>
  2) <strong>Capture (multiple) parts of text</strong><br><br></p>
  
  <p>In PHP I could just do something like this:<br><br></p>
  
  <pre>
  preg_match('#(word1).*(word2)#',$text,$matches);
  </pre>
  
  <p>I'm not sure how that would be possible in *nix...</p>