stew has asked for the wisdom of the Perl Monks concerning the following question:

Should be simple but I have a list of documents that have been titled in the format 'A Guide to Criminal Records Clearance for Prospective Employees' etc. This isn't much good to an end user looking down a long list if they all begin with 'A guide to .... etc'

What I want is a regex that will take the 'A guide to part off the front and tack it on to the end of the title so it's in the form 'Criminal Records Clearance for Prospective Employees - A Guide to'. Tried using s/(A guide to )(.*)/($2) ($1)/ but this doesn't work

Any ideas?

update (broquaint): title change (was Another RegEx poser)

Replies are listed 'Best First'.
Re: Another RegEx poser
by BrowserUk (Patriarch) on Nov 18, 2002 at 11:34 UTC

    The specific reason your regex didn't work is because you have "Guide" in the text and "guide" in the regex. You can get around this problem by using the /i option for case independant matching. You also don't need (and don't appear to want) the brackets on the replacement side of the s///.

    C:\test>perl -e "$_='A Guide to Criminal Records Clearance for Prospec +tive Employees'; s/(A guide to )(.*)/$2 - $1/i; print" Criminal Records Clearance for Prospective Employees - A Guide to
    Try that.

    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

      Minor change to remove the trailing space ;)
      $_='A Guide to Criminal Records Clearance for Prospective Employees'; s/(A\s+guide\s+to)\s+(.*)/$2 - $1/i; print;
      Criminal Records Clearance for Prospective Employees - A guide to

      Hope it helps
      UnderMine

Re: Another RegEx poser
by sch (Pilgrim) on Nov 18, 2002 at 11:34 UTC

    I suspect it's because you've got problems with the case of the text you're checking. If you try

    s/(a guide to )(.*)/$2 - $1/i
    it seems to work ok for me, since the /i tells the regex to ignore case.

    Hey, if there's code up ^^ there ^^, don't blame me if it doesn't work.

    But today you took me walking, Through a land that we have lost,
    While our children sit at websites, With no access to the cost

Re: Another RegEx poser
by Aristotle (Chancellor) on Nov 18, 2002 at 12:43 UTC
    Or you could try the lowtech approach.
    my $prefix = "A guide to"; s/^$prefix (.*)/$1 - $prefix/i;

    Makeshifts last the longest.

Re: Another RegEx poser
by fruiture (Curate) on Nov 18, 2002 at 11:31 UTC

    Define "this doesn't work"! Fatal error? Does your computer break down? Does perl say "I don't want to do that"?

    # will probably "work" s/^'A guide to ([^']+)'$/'$1 - A Guide to'/; # but for it's fixed length: substr( $_ , -1,0, ' '.substr( substr($_,1,11,'') ,0,10) );
    --
    http://fruiture.de