Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何从url中检查和提取单词_Regex_Go_Url_Word_Predefined Variables - Fatal编程技术网

Regex 如何从url中检查和提取单词

Regex 如何从url中检查和提取单词,regex,go,url,word,predefined-variables,Regex,Go,Url,Word,Predefined Variables,Go内置regex pkg的文档如下: Regex测试仪在Go-here: 我有一个预定义单词列表: christmas, santa, tree ( -> the order here is important. Check for words from left to right) 我正在尝试在不同的url字符串中检查上述单词之一: /api/container/:containerID/santa ( -> I want back santa) /api

Go内置regex pkg的文档如下: Regex测试仪在Go-here:

我有一个预定义单词列表:

christmas, santa, tree  ( -> the order here is important. Check for words from left to right)
我正在尝试在不同的url字符串中检查上述单词之一:

/api/container/:containerID/santa           ( -> I want back santa)
/api/tree/:containerID/                     ( -> I want back tree)
/api/tree/:containerID/christmas            ( -> I want back christmas, not tree)
我尝试过的正则表达式是:

re := regexp.MustCompile(`^(christmas)|(santa)|(tree)$`)
      fmt.Println("santa? ", string(re.Find([]byte(`/api/container/:containerID/santa`))))
      // output OK: santa? santa
      fmt.Println("tree? ", string(re.Find([]byte(`/api/tree/:containerID/`))))  
      // output FAIL/EMPTY: tree? 
      fmt.Println("christmas? ", string(re.Find([]byte(`/api/tree/:containerID/christmas`))))  
      // output FAIL/EMPTY: christmas? 
我也尝试过以下方法,但这会返回孔串,而不是我要查找的单词:

re := regexp.MustCompile(`^.*(christmas).*|.*(santa).*|.*(tree).*$`
      fmt.Println("santa? ", string(re.Find([]byte(`/api/container/:containerID/santa`))))
      // output FAIL/HOLE URL BACK: santa? /api/container/:containerID/santa
      fmt.Println("tree? ", string(re.Find([]byte(`/api/tree/:containerID/`))))  
      // output FAIL/FAIL/HOLE URL BACK: tree? /api/tree/:containerID/ 
      string(re.Find([]byte(`/api/tree/:containerID/christmas`))))  
      // output FAIL/FAIL/HOLE URL BACK: christmas? /api/tree/:containerID/christmas

我不知道regex引擎的最后一个表达式有什么问题,应该只记住paranthesis中的内容。

不要在这个任务中使用正则表达式。它过于复杂,难以推理,正如你现在所知道的那样,而且速度缓慢。一种更简单的方法是简单地在每个路径段上循环并查找匹配项:

针:=[]字符串{圣诞节,圣诞老人,圣诞树} sampleURL:=`/api/container/:containerID/santa` 对于_,部分:=范围字符串.SplitsampleURL,/{ 对于针:=范围针{ 如果零件==针{ fmt.printfound%s\n,指针 } } } 如果您有很多要搜索的单词,使用地图可能会提高效率:

针:=[]字符串{圣诞节、圣诞老人、树、驯鹿、铃铛、chior,/*可能还有数百个*/} NeederMap:=makemap[string]结构{},LenPineers 对于针:=范围针{ 针线图[针]=结构{}{} } sampleURL:=`/api/container/:containerID/santa` 对于_,部分:=范围字符串.SplitsampleURL,/{ 如果u,ok:=针线图[部分];ok{ fmt.printfound%s\n,指针 } }
您希望这些词是整个URL段,还是子字符串匹配?也就是说,你们想只匹配/tree/还是同时匹配/street/?另外,你们能解释一下为什么目标词的顺序很重要吗?你说是的,但是你的代码似乎没有注意到排序。我希望它们是整个url段。很好。这简化了事情。因此,最简单的解决方案是停止使用正则表达式正则表达式几乎总是错误的工具,而只是将路径拆分为多个分段,并循环遍历它们以查看是否有匹配项。regexp并不是适用于所有情况的正确工具。拆分URL路径和处理是很简单的,但我仍然很好奇为什么最后一个正则表达式不起作用。“你知道吗?”B先生,你最后的正则表达式没有意义。你认为。*^该怎么办?你不回答我的问题..*应该在^之后。