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!

Need some help parsing text

Status
Not open for further replies.

SensesFail

Programmer
Dec 9, 2008
3
US
I have strings of the form "CharNumCharCharChar", for example s5tgn, and I want to store the number, so for the example here I want to take s5tgn and store 5 in a variable.

Does anyone know how to do this in Perl?

Thanks!
 
What have you tried?

You should be able to do it fairly easily with a regex.
 
If the number is always in the same place (second character in string) just use the substr() function:

$st="s5tgn";
$nr=substr($st,1,1);
# substr(string,starting offset,length)

Avar Pentel
 
Hi

I think the OP forgot to specify it, but the input string is something like :
Code:
$st="foo f4oo s5tgn bar b6ar";
In which case [tt]substr[/tt] would not really help. But regular expression as always :
Code:
$n=$1 if $st=~m/[[:alpha:]]([[:digit:]])[[:alpha:]]{3}/;

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top