dongshen3352
dongshen3352
2017-07-27 19:15
浏览 48
已采纳

regexp.FindAll *的最大行长?

Is there a maximum length defined for text lines fed to the regexp.FindAll*Index() functions in the regexp package? Running the code below only provides the indices of the last 10 occurrences.

Go playground: https://play.golang.org/p/QgOw7TzuV4

package main

import (
    "fmt"
    "regexp"
)

func main() {
    line := `VAL_ Status 31 "31-Not Available" 30 "30-Not Defined" 29 "29-Not Defined" 28 "28-Received Temperature Msg" 27 "27 Temp Main (Sub-system)" 26 "26-Throttle Out of Correlation" 25 "25-Received Throttle Pos Msg" 24 "24-Received Throttle Position" 23 "23-Pedal Throttle Position" 22 "22-Throtttle Main (Sub-System)" 21 "21-Oil Makeup Pump Pressure" 20 "20-Engine/B-Average Speeds" 19 "19-A/D Power Regulator" 18 "18-Received Oil Temperature" 17 "17-Oil Temperature Main (IA)" 16 "16-B-Average Speed" 15 "15-Zero Stroke Position" 14 "14-Throttle Main (RVDT)" 13 "13-Engine Speed" 12 "12-Shift Mode Sel-Critical" 11 "11-Shift Mode Sel-Non Critical" 10 "10-Output Driver" 9 "9-Watchdog Off" 8 "8-Steer Gain Solenoid" 7 "7-Vehicle Driveability ID5" 6 "6-Vehicle Driveability ID4" 5 "5-Vehicle Driveability ID3" 4 "4-Vehicle Driveability ID2" 3 "3-Vehicle Driveability ID1" 2 "2-System Power" 1 "1-ROM" 0 "0-No Failures Detected" ;`
    re := regexp.MustCompile(" [0-9] ")

    fmt.Println(re.FindAllStringIndex(line, -1))
}

Yields:

[[677 680] [696 699] [722 725] [753 756] [784 787] [815 818] [846 849] [877 880] [896 899] [906 909]]

I've tried several of the FindAll*Index funcs and they're all the same.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongroufan6846
    dongroufan6846 2017-07-27 19:19
    已采纳

    There is no real limit on the number of matches. The reason your regex is not getting more matches is because it needs to be regexp.MustCompile(" [0-9]+ ") to match numbers with more than one digit, surrounded by spaces. Right now it only matches " 0 " through to " 9 ".

    点赞 评论

相关推荐