rsiedl has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I am trying to split a block of text that looks like this:
Into pairs of text before the "-" and text following.ABD - some text ACDB- some more text WD - more text - which spills onto the next line SD - another line
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 "-".$myHash{'ABD'} = "some text" $myHash{'ACDB'} = "some more text" etc.
What I think this matches is :/^([A-Z]{2,4})(\s{0,2})\-\s((.*)\n)*/g
Update:
Thanks very much for your help guys.
This is what I ended doing in the end:
Cheers,#!/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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl Regular Expressions
by kvale (Monsignor) on Mar 22, 2004 at 21:33 UTC | |
|
Re: Perl Regular Expressions
by cLive ;-) (Prior) on Mar 22, 2004 at 22:01 UTC | |
|
Re: Perl Regular Expressions
by neniro (Priest) on Mar 22, 2004 at 22:06 UTC | |
by CountZero (Bishop) on Mar 22, 2004 at 22:34 UTC | |
|
Re: Perl Regular Expressions
by tinita (Parson) on Mar 22, 2004 at 21:43 UTC | |
|
Re: Perl Regular Expressions
by BUU (Prior) on Mar 22, 2004 at 21:52 UTC |