Solved

Reg Exp - Get first three chars

Posted on 2011-03-04
14
501 Views
Last Modified: 2012-05-11
Hello -

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

thanks,
DT
0
Comment
Question by:DTwined
  • 5
  • 3
  • 2
  • +3
14 Comments
 
LVL 9

Expert Comment

by:McOz
Comment Utility
Instead of regexp, could you use something like:

$var = substr($string, 0, 3);
0
 

Author Comment

by:DTwined
Comment Utility

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
 
LVL 29

Expert Comment

by:Randy Downs
Comment Utility
Try this
my $color  = substr $s, 0, 3;

Example here - http://perldoc.perl.org/functions/substr.html
0
 

Author Comment

by:DTwined
Comment Utility

Number-1 -

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

thanks.
0
 
LVL 9

Accepted Solution

by:
McOz earned 500 total points
Comment Utility
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility

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

Open in new window

0
 
LVL 1

Expert Comment

by:notjames
Comment Utility

  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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
after  if ( /(?:^(.{3}))/ )
if ( $1 and $1 ne '' ) seems rather redundant
and  /(?:^(.{3}))/ seems like a complicated way of saying /(...)/
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Just curious, but how does the selected solution allow you to "grab" those characters? I must be missing something  : )
0
 
LVL 1

Expert Comment

by:notjames
Comment Utility
@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
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
 
LVL 1

Expert Comment

by:notjames
Comment Utility
@kaufmed: I don't understand. You're stating that you're agreeing with ozo, but you used my code (above).
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I modified your code to demonstrate ozo's point. Please take a second look  = )
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now