in reply to regex man they are tough

Is this search string a regexp or a normal string?

If not:
foreach $line ( @data ) { $line =~ s/ //gm; $first_part = substr($line, 0, 16); # get first 16 $idx = index( $line, "Search String", 16 ); $second_start = $idx > 30 ? $idx - 30 : 0; if ( $idx != -1 ) { $second_part = substr( $line, $second_start, 30 ); $third_part = substr( $line, $idx+length("Search String"), 30 ); } else { $second_part = $third_part = ""; } $line = ""; }
(untested)

Update: Added some bounds checking to avoid errors

Replies are listed 'Best First'.
Re^2: regex man they are tough
by tgolf4fun (Novice) on Apr 28, 2005 at 17:31 UTC
    the search string is a normal string sorry about that, but this is giving me ideas of which way to go. Trying out
Re^2: regex man they are tough
by cazz (Pilgrim) on Apr 29, 2005 at 14:56 UTC
    I prefer to use regexps instead of your example due to brevity of handling the failure cases. Even so, you can tweak yours to be a bit more resilant.

    This bit doesn't take into account not having 30 characters before the search string:

    $idx = index( $line, "Search String", 16 );
    Since we know there must be 30 bytes of data before the Search String, do this:
    $idx = index($line, "Search String", 16+30);
    The same idea is true for the "third_part" bit in your code and isn't hard to handle.
Re^2: regex man they are tough
by tgolf4fun (Novice) on Apr 29, 2005 at 16:59 UTC
    where does the scalar $index come from in your code above?
      The scalar $idx contains the return value from the builtin function index.