Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attention Regex gurus: Grabing first six character from string 2

Status
Not open for further replies.

thendal

Programmer
Aug 23, 2000
284
Hi all!

How i will be able to grab first six character from a string and then check the first 6 character string to have atleast two alphabetic charater and atleast one digit.

I tired something like this ...

$test="thisistest";
@array1=split(//,$test);
for($i=0;$i<6;$i++)
{
$sixcharacter=$sixcharacter.$array1[$i];
}

# I know there is much easier method than this..

# to check the atleast two alphabetic character and a digit

if((!($sixcharacter=~/\d/))&&(!($sixcharacter=~/[a-z]/i)))
{
print "Need atleast one digit & two alphabetic character");
}

I got the digit part correct , to check atleast two alphabetic character need a better regular experssion.

Any thoughts ...

Thank you.


 
one way (maybe not all that great):

Code:
my $test="thisistest";
my $string = substr $test,0,6;
my ($alpha,$digit) = (0,0);
$alpha++ while $string =~ m/[a-z]/g;
$digit++ while $string =~ m/[0-9]/g;
if ($digit >= 2 && $alpha >=2 ) {
   print "Good!";
}
else {
   print "No good!"
}
 
Try this:
my ($sixcharacter) = ($temp =~ /^(.{6})/);

if (($sixcharacter =~ m/\d/g) && ($sixcharacter =~ m/[a-zA-Z].*[a-zA-Z]/g)) {
print "Yes";
}
 
my example had an error, you only need one digit:

my $test="thisistest";
my $string = substr $test,0,6;
my ($alpha,$digit) = (0,0);
$alpha++ while $string =~ m/[a-z]/g;
$digit++ while $string =~ m/[0-9]/g;
if ($digit >= 1 && $alpha >=2 ) {
print "Good!";
}
else {
print "No good!"
}
 
awk
Code:
{ $0 = six = substr($0,1,6)
  if (/[0-9]/ && gsub(/[a-zA-Z]/,1)>1)
    print six,"is good"
}
 
Thank you very much.. You guys rock.

cntr, you kinda of finished in two lines i didn't understand $0 part..

 
Kevin, you may also want to add 'i'

$alpha++ while $string =~ m/[a-z]/ig;
covers both upper&lower case alphabets.

Thanks again.
:)

 
Its probably meant more visually.. $0 is the whole input of the current to process record in awk
Doing this saves doing six ~ /[0-9]/ and a third argument to gsub() (shouldnt it be >2 instead >1 there?)

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
nm >1 is right **selfslap

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top