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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if a string's first character is alphabet 1

Status
Not open for further replies.

rudorathod

IS-IT--Management
Jun 4, 2007
11
0
0
US
could someone give me the snippet where i can check if the first character in a string is a alphabet or not?

Thanks
 
Code:
if (/^[a-zA-Z]/) {
   starts with alpha
}
else {
   does not start with alpha
}




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
is this condition correct?

$firstserial==(/^[a-zA-Z]/)
 
Is this school work?

Code:
if ($foo =~ /^[a-zA-Z]/) {
   starts with alpha
}
else {
   does not start with alpha
}



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Does it apply for checking the first three characters being alphabets?

for example

ABC1111

and

AC11222


Thanks
 
You should do your own school work.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks Kevin,
I already did it, wanted to make sure if someone has a better idea. Its good to share the knowledge.

substr($SERIAL_NO,0,1)==(/^[a-zA-Z]/) and
substr($SERIAL_NO,1,1)==(/^[a-zA-Z]/) and substr($SERIAL_NO,2,1)==(/^[a-zA-Z]/)
 
That works - but I don't believe I understand why. If you add the -w switch, you'll see the interpreter complain some.

The example KevinADC presented will match the anything that starts w/ a to Z. So in retrospect of your 10:58AM question, it would match both of your examples. If you needed to differentiate, the following may be useful
Code:
if ($SERIAL_NO =~ /^[a-z][^a-z]/i) {
   # matches "A123"  "AA123"  "AAA123"
   #           YES      NO       NO
} elsif ($SERIAL_NO =~ /^[a-z]{2}[^a-z]/i) {
   # matches "A123"  "AA123"  "AAA123"
   #           NO      YES       NO
} elsif ($SERIAL_NO =~ /^[a-z]{3}[^a-z]/i) {
   # matches "A123"  "AA123"  "AAA123"
   #           NO       NO      YES
} elsif ($SERIAL_NO =~ /^[a-z]/i) {
   # matches "A123"  "AA123"  "AAA123"  "AAAAAAAAAA123"
   #           YES     YES      YES          YES
}
 
I already did it,

rudorathod,

Your code will not work correctly. I have red-flagged this thread though so it may be removed. Student posting is not allowed here for a reason.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
KevinADC - in case you're able to reply before this posting is removed: Out of curiosity, I tried the follwing
Code:
if (substr("A123",0,1)==(/^[A-Z]/)) { print "match\n" }
# above prints "match"
if (substr("1A123",0,1)==(/^[A-Z]/)) { print "match\n" }
# above prints nothing.
?? It appeared to work, but it doesn't make sense to me as to why. Would this be one of those scenarios where it's not supposed to work (it works by accident) so there's no reason trying to figure out why?
 
Hey so you guys are saying that it might not work for some scenarios?

because i tried with few examples and it works.

Kevin, this is not a school assignment or anything, so dont take it seriously.

here are few of the examples i tried,
i want to make sure it works so when i load the complete file everytime, it doesnt load the database improper.

SM89300234
TYW84930832
KPP150PSMC
1C71196592

Thanks
 
Next time please answer the question if what you are doing is school work.

Consider this:

Code:
if ('#' == (/^[a-zA-Z]/)) {
   print "They are equal";
}

The above simulates your code and prints "they are equal". The problem is: what are they equal to? This is how the expression is ultimately evaluated:

Code:
if (0 == ($_ =~ /^[a-zA-Z]/)) {
   print "They are equal";
}

Since "#" is not a number, it is given the value of 0 (zero) when you use the binary math operator '==' to compare it to something. And since $_ does not match /^[a-zA-Z]/ the return value of the regexp in scalar context is also 0 (zero). So they match.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ding (as the light goes on) Hence, had I used "0A123" in my example, it would provide incorrect results. Thanks.
 
Yes, your test was faulty. In this case:

Code:
if (substr("1A123",0,1)==(/^[A-Z]/)) { print "match\n" }

it evaluates as:

Code:
if (1 == 0) {}

which is false, but for the wrong reason.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top