与えられた正規表現パターンをテキスト文字列にマッチングします.
pattern が text にマッチした場合, start には文字列の何文字目から マッチしたのかを示す数値 (正の整数) が返ります. length には何文字分マッチしたのかを示す数値 (正の整数) が返ります.
マッチしない場合, length == -1, start == 0 となります.
program regex_test
implicit none
integer:: start, length
character(TOKEN) :: pattern, text
pattern = "->"
text = "time->0.0,x->hoge"
call match(trim(pattern), trim(text), start, length)
pattern = "^##+"
text = "####### hoge"
call match(trim(pattern), trim(text), start, length)
pattern = "@+$"
text = "# hoge @@@"
call match(trim(pattern), trim(text), start, length)
end program regex_test
シンプルな正規表現関数 'match' を提供します.
subroutine, public match(pattern, text, start, length)
integer, parameter, public token
単語やキーワードを保持する文字型変数の種別型パラメタ
469 implicit none
470 character(len = *), intent(in) :: pattern, text
471 integer, intent(out) :: start, length
472 integer, allocatable :: ipattern(:)
473 integer :: text_length
474 continue
475
476 if (len(pattern) <= 0) then
477 length = 0
478 start = 1
479 return
480 endif
481
482 allocate(ipattern(len(pattern) + 2))
483 call preprocess_pattern(pattern, ipattern)
484
485 if (ipattern(1) == sym_headfix) then
486 start = 1
487 call match_here(ipattern(2: ), text, length)
488 if (length < 0) goto 995
489 goto 999
490 end if
491
492 text_length = len(text)
493 do, start = 1, text_length + 1
494 call match_here(ipattern, text(start:text_length), length)
495 if (length >= 0) goto 999
496 end do
497
498995 continue
499 start = 0
500 length = -1
501999 continue
502 deallocate(ipattern)