如何获取Ruby字符串中正则表达式匹配的索引位置?

更新于
2026-10-03 20:19:36
0阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计170个文字,预计阅读时间需要1分钟。

如何从字符串中获取所有出现的正则表达式的索引数组?示例字符串:`example_string=hello how are you? 我想获得regexp /e/ 的数组 [1, 12]。这是获取匹配索引数组的一种方法:`example_string=hello how

如何在字符串中获取所有出现的正则表达式的索引(或位置)数组?

example_string= "hello how are you?

我想获得regexp / e /的数组[1,12]

这是获取匹配索引数组的一种方法:

example_string = "hello how are you?" example_string.enum_for(:scan, /e/).map { Regexp.last_match.begin(0) } # => [1, 12]

希望能帮助到你!

本文共计170个文字,预计阅读时间需要1分钟。

如何从字符串中获取所有出现的正则表达式的索引数组?示例字符串:`example_string=hello how are you? 我想获得regexp /e/ 的数组 [1, 12]。这是获取匹配索引数组的一种方法:`example_string=hello how

如何在字符串中获取所有出现的正则表达式的索引(或位置)数组?

example_string= "hello how are you?

我想获得regexp / e /的数组[1,12]

这是获取匹配索引数组的一种方法:

example_string = "hello how are you?" example_string.enum_for(:scan, /e/).map { Regexp.last_match.begin(0) } # => [1, 12]

希望能帮助到你!