• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

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
0
DTwined
Asked:
DTwined
  • 5
  • 3
  • 2
  • +3
1 Solution
 
McOzCommented:
Instead of regexp, could you use something like:

$var = substr($string, 0, 3);
0
 
DTwinedAuthor Commented:

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.
0
 
Randy DownsOWNERCommented:
Try this
my $color  = substr $s, 0, 3;

Example here - http://perldoc.perl.org/functions/substr.html
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
DTwinedAuthor Commented:

Number-1 -

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

thanks.
0
 
McOzCommented:
OK, how about:

$string =~ m/^LOG/;

Also, check out this link which gives some good info about perl regular expressions:

http://www.troubleshooters.com/codecorn/littperl/perlreg.htm#SimpleStringComparisons
0
 
käµfm³d 👽Commented:

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

Open in new window

0
 
notjamesCommented:

  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
0
 
ozoCommented:
after  if ( /(?:^(.{3}))/ )
if ( $1 and $1 ne '' ) seems rather redundant
and  /(?:^(.{3}))/ seems like a complicated way of saying /(...)/
0
 
käµfm³d 👽Commented:
Just curious, but how does the selected solution allow you to "grab" those characters? I must be missing something  : )
0
 
notjamesCommented:
@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.
0
 
käµfm³d 👽Commented:
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
0
 
käµfm³d 👽Commented:
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.
0
 
notjamesCommented:
@kaufmed: I don't understand. You're stating that you're agreeing with ozo, but you used my code (above).
0
 
käµfm³d 👽Commented:
I modified your code to demonstrate ozo's point. Please take a second look  = )
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now