• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • 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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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