简体   繁体   中英

Extracting numbers from string in perl

I'm having a problem coming up with an expression that would print out only "123" from "LOL123." I cannot use regex to solve this, so I must compare the string somehow. This is the code I have so far:

print join '', grep{$_ + 0} split //, "LOL123";

This works, but gives me a warning that L, O, L are nonnumeric.

Is there any possible way to do this without getting an error?

To split a string into a list of characters, use split // , not split / / .

Then the code in the grep needs to test whether $_ (which grep will alias to each character in turn) is a digit. Normally you'd use a regex for that, but you could use be string comparison operators ge and le .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM