sunty
2024-05-29 edb716edcd5b0ad3be47e9954f797c892c743fe6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package task
 
import (
    "fmt"
    "regexp"
    "strconv"
)
 
func extractFloorNumber(input string) (int, error) {
    // 定义匹配楼层数字的正则表达式
    re := regexp.MustCompile(`-?\d+`)
 
    // 查找第一个匹配的楼层数字
    matches := re.FindStringSubmatch(input)
 
    // 如果找到匹配的楼层数字
    if len(matches) > 0 {
        // 将匹配的楼层数字转换为整数
        floorNumber, err := strconv.Atoi(matches[0])
        if err != nil {
            return 0, err
        }
        return floorNumber, nil
    }
 
    // 如果未找到匹配的楼层数字,则返回错误
    return 0, fmt.Errorf("No floor number found in input: %s", input)
}