indexof_regex()

返回输入字符串中指定的查找正则表达式的第一个匹配项的索引(从零开始)。

请参阅 indexof()

语法

indexof_regex(string,match[,start[,length[,occurrence]]])

详细了解语法约定

参数

客户 类型​​ 必需 说明
字符串 string 要搜索的源字符串。
match string 正则表达式查询字符串。
start int 搜索开始位置。 如果是负值,则将从 string 的末尾起,将起始搜索位置偏移以下步数:abs(start)
length int 要检查的字符位置数。 值为 -1 表示长度没有限制。
occurrence int 出现的次数。 默认值为 1。

返回

match 的索引位置(从零开始)。

  • 如果在 string 中找不到 match,则返回 -1。
  • 如果符合以下条件,则返回 null
    • start 小于 0。
    • occurrence 小于 0。
    • length 小于 -1。

注意

  • 不支持重叠匹配查找。
  • 正则表达式字符串可能包含需要转义或使用 @'' 字符串文本的字符。

示例

print
    idx1 = indexof_regex("abcabc", @"a.c"), // lookup found in input string
    idx2 = indexof_regex("abcabcdefg", @"a.c", 0, 9, 2),  // lookup found in input string
    idx3 = indexof_regex("abcabc", @"a.c", 1, -1, 2),  // there's no second occurrence in the search range
    idx4 = indexof_regex("ababaa", @"a.a", 0, -1, 2), // Matches don't overlap so full lookup can't be found 
    idx5 = indexof_regex("abcabc", @"a|ab", -1)  // invalid start argument

输出

idx1 idx2 idx3 idx4 idx5
0 3 -1 -1