Solved

Reg Exp - Get first three chars

Posted on 2011-03-04
14
503 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
ID: 35036602
Instead of regexp, could you use something like:

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

Author Comment

by:DTwined
ID: 35036627

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

Example here - http://perldoc.perl.org/functions/substr.html
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:DTwined
ID: 35036661

Number-1 -

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

thanks.
0
 
LVL 9

Accepted Solution

by:
McOz earned 500 total points
ID: 35036674
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 75

Expert Comment

by:käµfm³d 👽
ID: 35036857

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

Open in new window

0
 
LVL 1

Expert Comment

by:notjames
ID: 35041800

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

Expert Comment

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

Expert Comment

by:käµfm³d 👽
ID: 35048097
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
ID: 35053457
@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 75

Expert Comment

by:käµfm³d 👽
ID: 35056118
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 75

Expert Comment

by:käµfm³d 👽
ID: 35056857
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
ID: 35113185
@kaufmed: I don't understand. You're stating that you're agreeing with ozo, but you used my code (above).
0
 
LVL 75

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

856 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