Really?!<div><br></div><div>Ugh.  I will acknowledge readily that I have to squint very hard to understand your regexs, Mark, but, I have called out Larry Wall for writing less terse stuff than this </div><div><br></div><div>
:)</div><div><br></div><div><br><br><div class="gmail_quote">On Mon, May 16, 2011 at 11:31 AM, Mark Senn <span dir="ltr"><<a href="mailto:mark@ecn.purdue.edu">mark@ecn.purdue.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">  > I have this bank of regexes in my code.<br>
  ><br>
  >             $requests2{ $request }->{ $href->{ accession_id } }<br>
  >                 ->{ library } =~ s/\W/\-/g ;<br>
  >             $requests2{ $request }->{ $href->{ accession_id } }<br>
  >                 ->{ library } =~ s/_/\-/g ;<br>
  >             $requests2{ $request }->{ $href->{ accession_id } }<br>
  >                 ->{ library } =~ s/-+/-/g ;<br>
  >             $requests2{ $request }->{ $href->{ accession_id } }<br>
  >                 ->{ library } =~ s/-$//g ;<br>
  ><br>
  > I'm thinking that I can simplify this a lot.<br>
  ><br>
  > Change the \W and _ regexes to \W+ and _+ and you reduce the need for<br>
  > the s/-+/-/, so we can reduce it, I think, to<br>
  ><br>
  >             $requests2{ $request }->{ $href->{ accession_id } }<br>
  >                 ->{ library } =~ s/[\W_-]+/\-/g ;<br>
  ><br>
  > I'd have to bash that regex before I'm comfortable putting it into<br>
  > production.<br>
  ><br>
  > But the last part, getting rid of dashes at the end of a string, can I<br>
  > roll that into the bigger regex? I'm not seeing how right now.<br>
<br>
</div>I prefer the second of the three solutions below.<br>
<br>
#!/usr/local/bin/perl<br>
<br>
$s = '!@#$%^&*(){}--__--}{)(*&^%$#@!abc--';<br>
$_ = $S . "\n" . $s . "\n";<br>
s/\W/\-/g;<br>
s/_/\-/g;<br>
s/-+/-/g;<br>
s/-$//g;<br>
print "$_\n";<br>
<br>
$s = '!@#$%^&*(){}--__--}{)(*&^%$#@!abc--';<br>
$_ = $S . "\n" . $s . "\n";<br>
s/[\W_]/-/g;  # Change nonword or '_' characters to '-' everywhere.<br>
              # This will change any newlines to '-'.<br>
s/-+/-/g;     # Change two or more consecutive '-' characters<br>
              # to one '-' everywhere.<br>
s/-$//;       # Delete any '-' at the end of the string.<br>
print "$_\n";<br>
<br>
$s = '!@#$%^&*(){}--__--}{)(*&^%$#@!abc--';<br>
$_ = $S . "\n" . $s . "\n";<br>
s/[\W_-]+/-/g;  # Change consecutive nonword, '_', or '-'  characters<br>
                # to '-' everywhere.<br>
                # This will change any newlines to '-'.<br>
s/-$//;         # Delete any '-' at the end of the string.<br>
print "$_\n";<br>
<font color="#888888"><br>
-mark<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Purdue-pm mailing list<br>
<a href="mailto:Purdue-pm@pm.org">Purdue-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/purdue-pm" target="_blank">http://mail.pm.org/mailman/listinfo/purdue-pm</a><br>
</div></div></blockquote></div><br></div>