×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Using index to find alpabetic characters in string

Using index to find alpabetic characters in string

Using index to find alpabetic characters in string

(OP)
Hello! I'm trying to extract the numeric digits from a string which may contain both numeric and alpha characters. For example,

$wd = '16TH';
$tmp = index($wd, /[A-Z]/); # this doesn't work?
if ($tmp != -1){
    $wd = substr($wd,0,$tmp);
}
but this does!
$wd =~ m/[A-Z]/g;
$t1 = pos $wd;
if ($t1 > 0){
     $wd = $`;
}
I'm just wondering if regular expressions don't work for index. I know they work for split. What I don't like about the example that does work, is that if the string doesn't contain any alpha characters, $t1 is undefined? What am I missing here?
Thanks

Dabits

RE: Using index to find alpabetic characters in string

here's a regex approach

#!/usr/local/bin/perl
$string = 'a1b2c3d4e5f6g7';
while ($string=~ /\d/g)
    {
    $number = $&;    
# '$&' catches the matched chars
    print "Found $number\n";
    }


The above code should print.....
Found 1
Found 2
Found 3
Found 4
Found 5
Found 6
Found 7



'Hope this helps.

RE: Using index to find alpabetic characters in string

(OP)
Thanks..

The regex code does also work. I was just wondering about index.

Dabits

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close