本文介绍了在 Perl 中,如何判断字符串是否为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Perl 将一些 XML 转换为 JSON.如果 XML 属性是一个数字,我不想在它周围加上引号,以便 JSON 将其视为数字而不是字符串.如何判断 Perl 字符串是否为数字(仅包含数字 0 到 9,可能还有一个小数点)?
I am using Perl to convert some XML to JSON. If the XML attribute is a number, I don't want to put quotes around it so that JSON will treat it as a number and not a string. How can I tell if a Perl string is a number (contains only numbers 0 through 9 and possibly one decimal point)?
推荐答案JSON 规范 提供了相当明确的格式规则一个数字,所以下面的正则表达式应该可以工作:
The JSON specification provides fairly clear rules on the format of a number, so the following regex should work:
/^-?(0|([1-9][0-9]*))(\.[0-9]+)?([eE][-+]?[0-9]+)?$/更多推荐
在 Perl 中,如何判断字符串是否为数字?
发布评论