Loading...
Searching...
No Matches
Functions/Subroutines
dc_regex Module Reference

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

Functions/Subroutines

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

Detailed Description

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

Author
Youhei SASAKI

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 
)
Parameters
[in]pattern
program regex_test
use dc_regex, only: match
use dc_types, only: token
implicit none
integer:: start, length
character(TOKEN) :: pattern, text
continue
pattern = "->"
text = "time->0.0,x->hoge"
call match(trim(pattern), trim(text), start, length)
call formatted_print
pattern = "^##+"
text = "####### hoge"
call match(trim(pattern), trim(text), start, length)
call formatted_print
pattern = "@+$"
text = "# hoge @@@"
call match(trim(pattern), trim(text), start, length)
call formatted_print
contains
subroutine formatted_print
use dc_string, only: printf
call printf(fmt='pattern= %c : text= %c : start= %d : length= %d', &
& c1=trim(pattern), c2=trim(text), i=(/start, length/))
end subroutine formatted_print
end program regex_test
シンプルな正規表現関数 'match' を提供します.
Definition dc_regex.f90:16
subroutine, public match(pattern, text, start, length)
Definition dc_regex.f90:267
種別型パラメタを提供します。
Definition dc_types.f90:49
integer, parameter, public token
単語やキーワードを保持する文字型変数の種別型パラメタ
Definition dc_types.f90:109
このプログラムを実行することで以下の出力が得られるはずです。
 pattern= -> : text= time->0.0,x->hoge : start= 5 : length= 2
 pattern= ^##+ : text= ####### hoge : start= 1 : length= 7
 pattern= @+$ : text= # hoge @@@ : start= 8 : length= 3
[in]text
program regex_test
use dc_regex, only: match
use dc_types, only: token
implicit none
integer:: start, length
character(TOKEN) :: pattern, text
continue
pattern = "->"
text = "time->0.0,x->hoge"
call match(trim(pattern), trim(text), start, length)
call formatted_print
pattern = "^##+"
text = "####### hoge"
call match(trim(pattern), trim(text), start, length)
call formatted_print
pattern = "@+$"
text = "# hoge @@@"
call match(trim(pattern), trim(text), start, length)
call formatted_print
contains
subroutine formatted_print
use dc_string, only: printf
call printf(fmt='pattern= %c : text= %c : start= %d : length= %d', &
& c1=trim(pattern), c2=trim(text), i=(/start, length/))
end subroutine formatted_print
end program regex_test
このプログラムを実行することで以下の出力が得られるはずです。
 pattern= -> : text= time->0.0,x->hoge : start= 5 : length= 2
 pattern= ^##+ : text= ####### hoge : start= 1 : length= 7
 pattern= @+$ : text= # hoge @@@ : start= 8 : length= 3

Definition at line 266 of file dc_regex.f90.

267 !
279 !
320 implicit none
321 character(len = *), intent(in):: pattern, text
322 integer, intent(out):: start, length
323 integer, allocatable:: ipattern(:)
324 integer:: text_length
325 continue
326 ! 空 pattern は空文字列に適合
327 if (len(pattern) <= 0) then
328 length = 0
329 start = 1
330 return
331 endif
332 ! メタキャラクタの認識
333 allocate(ipattern(len(pattern) + 2))
334 call preprocess_pattern(pattern, ipattern)
335 ! 頭寄せ指定のある場合
336 if (ipattern(1) == sym_headfix) then
337 start = 1
338 call match_here(ipattern(2: ), text, length)
339 if (length < 0) goto 995
340 goto 999
341 endif
342 ! 最左原理
343 text_length = len(text)
344 do, start = 1, text_length + 1
345 call match_here(ipattern, text(start:text_length), length)
346 if (length >= 0) goto 999
347 end do
348 ! みつからない場合
349995 continue
350 start = 0
351 length = -1
352999 continue
353 deallocate(ipattern)