4.83 regexpos - Match Position

Format 1: regexpos($str$,regular expression)

Format 2: regexposw($str$,regular expression)

Return the starting position of the longest substring where the defined regular expression matches with the character string $str$. The first character of the string starts at position 0. The function return NULL if no match is found. Use the regexposw function if $str$ or regular expression contain multibyte characters.

Examples

Example 1: Basic Example

Find out the position of the longest substring that matches the regular expression c.*a. Note that the first character starts at position 0. Since the same input data and substring are used in the regexstr function, it is easy to compare the results and understand the differences.

$ more dat1.csv
id,str
1,xcbbbayy
2,xxcbaay
3,
4,bacabbca
$ mcal c='regexpos($s{str},"c.*a")' a=rsl i=dat1.csv o=rsl1.csv
#END# kgcal a=rsl c=regexpos($s{str},"c.*a") i=dat1.csv o=rsl1.csv
$ more rsl1.csv
id,str,rsl
1,xcbbbayy,1
2,xxcbaay,2
3,,
4,bacabbca,2

Example 2: Multibyte character

Find out the longest substring that matches the regular expression "い.*あ". However, since regexpos does not support multibyte characters, the function returns the position of bytes instead of characters.

$ more dat2.csv
id,str
1,漢漢あbbbいyy
2,漢あbいいy
3,
4,bあいあbbいあ
$ mcal c='regexpos($s{str},"あ.*い")' a=rsl i=dat2.csv o=rsl2.csv
#END# kgcal a=rsl c=regexpos($s{str},"あ.*い") i=dat2.csv o=rsl2.csv
$ more rsl2.csv
id,str,rsl
1,漢漢あbbbいyy,6
2,漢あbいいy,3
3,,
4,bあいあbbいあ,1

Example 3: Multibyte character 2

Find out the longest substring that matches the regular expression "い.*あ". The regexposw function is able to process multibyte characters accurately.

$ mcal c='regexposw($s{str},"あ.*い")' a=rsl i=dat2.csv o=rsl3.csv
#END# kgcal a=rsl c=regexposw($s{str},"あ.*い") i=dat2.csv o=rsl3.csv
$ more rsl3.csv
id,str,rsl
1,漢漢あbbbいyy,6
2,漢あbいいy,3
3,,
4,bあいあbbいあ,1