Link to home
Start Free TrialLog in
Avatar of DTwined
DTwined

asked on

Reg Exp - Get first three chars

Hello -

I need a Perl Regular Expression that will allow me to grab the first three characters from a string.

thanks,
DT
Avatar of McOz
McOz

Instead of regexp, could you use something like:

$var = substr($string, 0, 3);
Avatar of DTwined

ASKER


No, I need a regular expression.
I could also go with a perl based regular expression that will see if the first three chars match the string 'LOG'. The match should also be capitalized.
Avatar of Randy Downs
Try this
my $color  = substr $s, 0, 3;

Example here - http://perldoc.perl.org/functions/substr.html
Avatar of DTwined

ASKER


Number-1 -

I need a regular expression. Please see my first reponse.

thanks.
ASKER CERTIFIED SOLUTION
Avatar of McOz
McOz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

$source =~ m/^(.{3})/;
$extracted = $1;

Open in new window


  1 #!/usr/bin/perl -w
  2
  3 use strict;
  4
  5 while ( <DATA> )
  6 {
  7     chomp();
  8     my $val = 'Empty';
  9
 10     if ( /(?:^(.{3}))/ )
 11     {
 12         if ( $1 and $1 ne '' )
 13         {
 14             $val = $1;
 15         }
 16     }
 17
 18     print 'val is: '.$val,"\n";
 19 }
 20
 21 __DATA__
 22 this
 23 is
 24
 25 a test
 26 now to
 27 if this
 28
 29 works.
 30 Well,
 31 does it?



]$ ./test.pl
val is: thi
val is: Empty
val is: Empty
val is: a t
val is: now
val is: if
val is: Empty
val is: wor
val is: Wel
val is: doe
after  if ( /(?:^(.{3}))/ )
if ( $1 and $1 ne '' ) seems rather redundant
and  /(?:^(.{3}))/ seems like a complicated way of saying /(...)/
Just curious, but how does the selected solution allow you to "grab" those characters? I must be missing something  : )
@ozo:

Try removing the conditional and check the output. You'll see that the conditional is required the way I wrote the code.

The reason I use the (?:...) construct is primarily personal preference but is based on Perl goodness found in http://perldoc.perl.org/perlre.html#Extended-Patterns. I've grown to use the extended pattern use as a good habit building tool in case I want to ever use extended patterns in my REs down the line.
I'm going to have to agree with ozo (see attached). The only way you can get to your second condition is if the first condition (the regex match operation) is true; if the first condition is true, then you would trivially have something in the first capture group since you made capturing parentheses a part of your pattern. Also, since your pattern specifies 3 characters after the start of the line, having an empty match should not be possible.
Screenshot.png
As to ozo's point about the non-grouping construct, I think what she was implying is that there is not a real need to use that construct here, as you have no quantifiers on that pattern inside of the group. Using the capturing parens alone, as I did above ( http:#35036857 ) should be sufficient.
@kaufmed: I don't understand. You're stating that you're agreeing with ozo, but you used my code (above).
I modified your code to demonstrate ozo's point. Please take a second look  = )