gtool5 Fortran 90/95 ライブラリ 1.0.0-rc5
English
Loading...
Searching...
No Matches
dc_regex Module Reference

シンプルな正規表現関数 'match' を提供します. More...

Functions/Subroutines

subroutine, public match (pattern, text, start, length)

Detailed Description

シンプルな正規表現関数 'match' を提供します.

Author
Youhei SASAKI

このモジュールはシンプルな正規表現マッチング関数を提供します.

サポートする機能

パターン 説明
. 任意の1文字
? 直前の0回または1回
+ 直前の1回以上
* 直前の0回以上
^ 文字列の先頭
$ 文字列の末尾
[abc] 文字クラス
[^abc] 否定文字クラス
#d 数字 (0-9)
#a アルファベット
#w 単語文字 (英数字 + _)
#s 空白文字
#z 16進数字
#xHH 16進文字コード
Note
エスケープ文字は '\' ではなく '#' です.

Function/Subroutine Documentation

◆ match()

subroutine, public dc_regex::match ( character(len = *), intent(in) pattern,
character(len = *), intent(in) text,
integer, intent(out) start,
integer, intent(out) length )

正規表現パターンをテキストにマッチング

与えられた正規表現パターンをテキスト文字列にマッチングします.

pattern が text にマッチした場合, start には文字列の何文字目から マッチしたのかを示す数値 (正の整数) が返ります. length には何文字分マッチしたのかを示す数値 (正の整数) が返ります.

マッチしない場合, length == -1, start == 0 となります.

Parameters
[in]pattern正規表現パターン
[in]text検索対象のテキスト文字列
[out]startマッチの開始位置 (マッチしない場合は 0)
[out]lengthマッチした長さ (マッチしない場合は -1)

program regex_test
use dc_regex, only: match
use dc_types, only: token
implicit none
integer:: start, length
character(TOKEN) :: pattern, text
pattern =
text =
call match(trim(pattern), trim(text), start, length)
! 結果: start=5, length=2
pattern =
text =
call match(trim(pattern), trim(text), start, length)
! 結果: start=1, length=7
pattern =
text =
call match(trim(pattern), trim(text), start, length)
! 結果: start=8, length=3
end program regex_test
シンプルな正規表現関数 'match' を提供します.
Definition dc_regex.f90:62
subroutine, public match(pattern, text, start, length)
Definition dc_regex.f90:469
種別型パラメタを提供します。
Definition dc_types.f90:55
integer, parameter, public token
単語やキーワードを保持する文字型変数の種別型パラメタ
Definition dc_types.f90:128

Definition at line 468 of file dc_regex.f90.

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 ! 空 pattern は空文字列に適合
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)