Hi Monks,

I am trying to split a block of text that looks like this:

ABD - some text ACDB- some more text WD - more text - which spills onto the next line SD - another line
Into pairs of text before the "-" and text following.

i.e.
$myHash{'ABD'} = "some text" $myHash{'ACDB'} = "some more text" etc.
The text before the "-" is always 4 characters, at least 2 A-Z's and always uppercase, and there is always a space after the "-".

So far what I have is :

/^([A-Z]{2,4})(\s{0,2})\-\s((.*)\n)*/g
What I think this matches is :
Start of line
2 - 4 capital alpha characters
0 - 2 whitespaces characters
A hyphen
A whitespace
( Any character any number of time followed by a newline ) any number of times

But it isnt doing what I thought it would so I must have something wrong :-)

Any help you could throw my way would be greatly appreciated.

Cheers,
rsiedl

"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us." Calvin-The Indispensable Calvin and Hobbes

Update:

Thanks very much for your help guys.
This is what I ended doing in the end:

#!/usr/bin/perl use strict; use warnings; # The data my $data =<< "END"; ABD - some text ACDB- some more text WD - more text - which spills onto the next line SD - another line END # Search and replace a newline followed by four spaces # followed by a hyphen with nothing $data =~ s/\n(\s{4})-//ig; # Split the data into lines my @lines = split(/\n/, $data); # Scroll though the lines foreach my $line ( @lines ) { # start-foreach # Check to make sure our line is formatted correctly if ($line =~ /^(\S+)\s*-\s+(.+)$/) { # start-if # Get the values out of the regex my ( $key, $value ) = $line =~ /^(\S+)\s*-\s+(.+)$/; # Print the results print "$key, $value\n"; } # end-if # Else line is not formatted correctly else { # start-else print "Badly formatted line!\n"; } # end-else } # end-foreach
Cheers,
rsiedl.

In reply to Perl Regular Expressions by rsiedl

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.