$chapter =~ s/\D+//g; # remove all non-digit characters # or perhaps to avoid the /g flag, (I wouldn't code it this way # because it is overly complex) however: $chapter =~ s/^(\D*)(\d+)(\D*)/$2/; # remove all optional non-digit stuff # before or after the digits # try the above with "XX546YYY", just "453ZZ" and "AAA123ZZZ77548" as # cases to probe the limits... what happens if it is not just "11VI"?