正则表达式的`FindAll*`方法中的最大行长度是多少?

huangapple go评论136阅读模式
英文:

maximum line length for regexp.FindAll*?

问题

regexp.FindAll*Index()函数在regexp包中用于查找文本行中的匹配项,并返回它们的索引位置。对于这些函数,没有明确定义文本行的最大长度限制。根据你提供的代码和输出结果,这些函数似乎只返回最后10个匹配项的索引位置。

你尝试了几个FindAll*Index函数,它们的行为都是相同的。

英文:

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

得分: 4

没有真正的匹配数量限制。你的正则表达式没有得到更多的匹配是因为它需要是regexp.MustCompile(" [0-9]+ "),以匹配由空格包围的多位数。目前它只匹配" 0 "" 9 "

英文:

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 ".

huangapple
  • 本文由 发表于 2017年7月28日 03:15:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/45359250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定