gtool5 Fortran 90/95 Library 1.0.0-rc5
日本語
Loading...
Searching...
No Matches
Functions/Subroutines
dc_regex Module Reference

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

Functions/Subroutines

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

Detailed Description

Provides 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 
)

Match regular expression pattern against text

Matches the given regular expression pattern against the text string.

If pattern matches text, start returns the position (1-based) where the match begins, and length returns the number of matched characters.

If no match is found, length == -1 and start == 0.

Parameters
[in]patternRegular expression pattern
[in]textText string to search
[out]startStarting position of match (0 if no match)
[out]lengthLength of match (-1 if no match)

Example

program regex_test
use dc_regex, only: match
use dc_types, only: token
implicit none
integer:: start, length
character(TOKEN) :: pattern, text
pattern = "->"
text = "time->0.0,x->hoge"
call match(trim(pattern), trim(text), start, length)
! Result: start=5, length=2
pattern = "^##+"
text = "####### hoge"
call match(trim(pattern), trim(text), start, length)
! Result: start=1, length=7
pattern = "@+$"
text = "# hoge @@@"
call match(trim(pattern), trim(text), start, length)
! Result: start=8, length=3
end program regex_test
Provides simple regular expression subroutine: 'match'.
Definition dc_regex.f90:62
subroutine, public match(pattern, text, start, length)
Definition dc_regex.f90:469
Provides kind type parameter values.
Definition dc_types.f90:55
integer, parameter, public token
Character length for word, 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)