Hey alexiskb,

I may be approaching this more from a mind-set of how to make it fast rather than how to make it elegant, but this is what I would try.

First I would using the quoting REx operator to precompile the RExs outside of the function. I also switched your parens to the non capturing format: (?:...) since you aren't using $1 in any of your examples. Lastly this is a case where using study might significantly speed your program. Here's what I came up with. Update 2: wait - why use parens at all? Silly me...

#! /usr/local/bin/perl -w use strict; my $REs = [ qr/(?: EUR1)[^,]+/, qr/(?: EUR2)[^,]+/, qr/(?: EUR3)[^,]+/, qr/(?: EUR4)[^,]+/, qr/(?: EUR8)[^,]+/, qr/(?: EUR0\.)[^,]+/, qr/(?: CHF10)[^,]+/, qr/(?: Y5)[^,]+/, qr/(?: NV )[^,]+/, qr/(?:NON-CUM)[^,]+/, qr/(?: LTD)[^,]+/, qr/(?: FIN )[^,]+/, qr/(?: INTL)[^,]+/, qr/(?:\$)[^,]+/, qr/(?:\s+$)/ ]; my $string = "a INTL , b Y5c, NV , d e f... & & CO FIN "; my $return = &format($REs, $string); print ">$string<\n"; print ">$return<\n"; exit; sub format { my $REs = shift; my $string = shift; study $string; $string =~ s/(?: & CO )[^,]+/ AND CO/; $string =~ s/&/AND/g; for (@{$REs}) { $string =~ s/$_//; } return $string; }
Without a sample of your data and output it is hard to be sure that this does what you want. It should, however, be faster, and the format subroutine is a little cleaner to look at.

Hope this helps you. If I have time I may run this through some benchmarks and see if it does in fact speed things up, and see which part helps the most.

Update: Oops - typos.

Good luck,
{NULE}
--
http://www.nule.org


In reply to Re: Multiple Regex, it works but it aint clever by {NULE}
in thread Multiple Regex, it works but it aint clever by alexiskb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.