indexof_regex()indexof_regex()

函数会报告输入字符串中指定的字符串第一次出现时的索引(从零开始)。Function reports the zero-based index of the first occurrence of a specified string within the input string. 纯字符串匹配项不重叠。Plain string matches don't overlap.

请参阅 indexof()See indexof().

语法Syntax

indexof_regex(source,lookup[,start_index[,length[,occurrence]]])indexof_regex(source,lookup[,start_index[,length[,occurrence]]])

自变量Arguments

参数Arguments 描述Description 必需还是可选Required or Optional
sourcesource 输入字符串Input string 必须Required
查找lookup 要查找的字符串String to seek 必须Required
start_indexstart_index 搜索开始位置Search start position 可选Optional
lengthlength 要检查的字符位置数。Number of character positions to examine. -1 定义无限长度-1 defines an unlimited length 可选Optional
occurrenceoccurrence 查找模式第 N 次出现时的索引。Find the index of the N-th appearance of the pattern.
默认值为 1(第一次出现时的索引)Default is 1, the index of the first occurrence 可选Optional

返回Returns

查找的从零开始的索引位置。Zero-based index position of lookup .

  • 如果在输入中找不到该字符串,则返回 -1。Returns -1 if the string isn't found in the input.
  • 在以下情况下返回 null:Returns null if:
    • start_index 小于 0。start_index is less than 0.
    • 出现次数小于 0。occurrence is less than 0.
    • 长度参数小于 -1。length parameter is less than -1.

示例Examples

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 is no second occurrence in the search range
 , idx4 = indexof_regex("ababaa", "a.a", 0, -1, 2)  // Plain string matches do not overlap so full lookup can't be found
 , idx5 = indexof_regex("abcabc", "a|ab", -1)  // invalid input
idx1idx1 idx2idx2 idx3idx3 idx4idx4 idx5idx5
00 33 -1-1 -1-1