Functions/Subroutines
dc_regex Module Reference

Provide simple regular expression subroutine: 'match'. More...

Functions/Subroutines

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

Detailed Description

Provide simple regular expression subroutine: '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
このプログラムを実行することで以下の出力が得られるはずです。
 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 267 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  ! みつからない場合
349 995 continue
350  start = 0
351  length = -1
352 999 continue
353  deallocate(ipattern)