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

All,
A. Would like to split the following word Camedia C-730 UltraZoom\UZoom/UZ (White) using multiple conditions using one line command. The conditions to split are
1. Space
2. -
3. /
4. \
5. (
6. )
After splitting the output should be
1. Camedia
2. C
3. 730
4. UltraZoom
5. UZoom
6. UZ
7. White


B. Also would like to know on how to REGEX a value either forward or backward from the position of the found required pattern. AN example
Acer Laptop XXYYY00MM 2.0 Ghz is an amazing piece.

In this example the pattern I look for is Ghz and would like to REGEX the words before it i.e. Acer Laptop XXYYY00MM 2.0 and also include the pattern (pattern may be anyword) in the final output.

Thanks for your help.

Replies are listed 'Best First'.
Re: Multiple condition split
by Corion (Patriarch) on Aug 16, 2007 at 11:38 UTC

    See perlre. The first question is answered by "character classes". For example, [/()] will match a forward slash, an opening parenthesis or a closing parenthesis.

    my @words = split m![/()]!, $line;

    The second question can be answered for example by using positive lookahead. For example, (?=Ghz) will make sure that there is the word Ghz following the current position.

    if ($line =~ m!(.*)(?=Ghz)!) { print "Found $1 with a 'Ghz' specification\n"; };

    Thinking about it, you don't even need the positive lookahead.

Re: Multiple condition split
by moritz (Cardinal) on Aug 16, 2007 at 11:36 UTC
    For A: split m{[ /\\()-]+}, $string;

    For B:

    if ($string =~ m/\A(.*?)Ghz/s) { print "Everyting up to Ghz: $1\n"; }

    If you want to inclde the Ghz in the output string, move the closing paren to the end: m/\A(.*?Ghz)/s

    Update: Added the + quantifier to the split regex

Re: Multiple condition split
by FunkyMonk (Bishop) on Aug 16, 2007 at 11:48 UTC
    A. Use split with a regex that uses a character class (see Regular Expressions).

    B. Use the match operator m// to capture (see perlretut and perlre) the bit you're interested in.

    my $str_A = q{Camedia C-730 UltraZoom\UZoom/UZ (White)}; my @words_A = split m{[-\\)\(/ ]+}, $str_A; print "$_\n" for @words_A; my $str_B = q{Acer Laptop XXYYY00MM 2.0 Ghz is an amazing piece}; my ( $words_B ) = $str_B =~ m{(.*Ghz)}; print "\n$words_B\n";

    Outputs:

    Camedia C 730 UltraZoom UZoom UZ White Acer Laptop XXYYY00MM 2.0 Ghz

Re: Multiple condition split
by graff (Chancellor) on Aug 17, 2007 at 02:47 UTC
    As you study the perlre manual and try out various things, you might consider whether task A would best be done like this:
    my @words = split /\W+/, $string;
Re: Multiple condition split
by akho (Hermit) on Aug 16, 2007 at 11:39 UTC
    umm…
    A:
    split /[\s\/\\\(\)-]/, ...;
    B: study perldoc perlre. You need look-ahead assertions.
Re: Multiple condition split
by Anonymous Monk on Aug 19, 2007 at 15:16 UTC
    All, Thanks a lot for your help.. Regards, S