2019-11-02 12:07:41 +08:00
|
|
|
|
# 20. 表示数值的字符串
|
|
|
|
|
|
2021-03-23 02:48:19 +08:00
|
|
|
|
[牛客网](https://www.nowcoder.com/practice/e69148f8528c4039ad89bb2546fd4ff8?tpId=13&tqId=11206&tab=answerKey&from=cyc_github)
|
2019-11-02 12:07:41 +08:00
|
|
|
|
|
|
|
|
|
## 题目描述
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
true
|
|
|
|
|
|
|
|
|
|
"+100"
|
|
|
|
|
"5e2"
|
|
|
|
|
"-123"
|
|
|
|
|
"3.1416"
|
|
|
|
|
"-1E-16"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
false
|
|
|
|
|
|
|
|
|
|
"12e"
|
|
|
|
|
"1a3.14"
|
|
|
|
|
"1.2.3"
|
|
|
|
|
"+-5"
|
|
|
|
|
"12e+4.3"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 解题思路
|
|
|
|
|
|
|
|
|
|
使用正则表达式进行匹配。
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
[] : 字符集合
|
|
|
|
|
() : 分组
|
|
|
|
|
? : 重复 0 ~ 1 次
|
|
|
|
|
+ : 重复 1 ~ n 次
|
|
|
|
|
* : 重复 0 ~ n 次
|
|
|
|
|
. : 任意字符
|
|
|
|
|
\\. : 转义后的 .
|
|
|
|
|
\\d : 数字
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```java
|
2021-03-23 02:48:19 +08:00
|
|
|
|
public boolean isNumeric (String str) {
|
|
|
|
|
if (str == null || str.length() == 0)
|
2019-11-02 12:07:41 +08:00
|
|
|
|
return false;
|
|
|
|
|
return new String(str).matches("[+-]?\\d*(\\.\\d+)?([eE][+-]?\\d+)?");
|
|
|
|
|
}
|
|
|
|
|
```
|