Loading...
Searching...
No Matches
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 =
call match(trim(pattern), trim(text), start, length)
call formatted_print
pattern =
text =
call match(trim(pattern), trim(text), start, length)
call formatted_print
pattern =
text =
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
Provide simple regular expression subroutine: 'match' .
Definition dc_regex.f90:16
subroutine, public match(pattern, text, start, length)
Definition dc_regex.f90:267
Provides kind type parameter values.
Definition dc_types.f90:49
integer, parameter, public token
Character length for word, 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 =
call match(trim(pattern), trim(text), start, length)
call formatted_print
pattern =
text =
call match(trim(pattern), trim(text), start, length)
call formatted_print
pattern =
text =
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 !
268 !> _pattern_ には正規表現を与えます。
269 !> _text_ には正規表現によって探査したい文字列を与えます。
270 !>
271 !> _pattern_ が _text_ にマッチした場合、
272 !> _start_ には文字列の何文字目からマッチしたのかを示す数値 (正の整数)
273 !> が返ります。
274 !> _length_ には何文字分マッチしたのかを示す数値 (正の整数)
275 !> が返ります。
276 !>
277 !> マッチしない場合、 length == -1, start == 0 となります。
278 !>
279 !
280 !> 例
281 !>~~~~~~~~~~~~~~~{.f90}
282 !> program regex_test
283 !> use dc_regex, only: match
284 !> use dc_types, only: TOKEN
285 !> implicit none
286 !>
287 !> integer:: start, length
288 !> character(TOKEN) :: pattern, text
289 !> continue
290 !> pattern = "->"
291 !> text = "time->0.0,x->hoge"
292 !> call match(trim(pattern), trim(text), start, length)
293 !> call formatted_print
294 !>
295 !> pattern = "^##+"
296 !> text = "####### hoge"
297 !> call match(trim(pattern), trim(text), start, length)
298 !> call formatted_print
299 !>
300 !> pattern = "@+$"
301 !> text = "# hoge @@@"
302 !> call match(trim(pattern), trim(text), start, length)
303 !> call formatted_print
304 !>
305 !> contains
306 !> subroutine formatted_print
307 !> use dc_string, only: Printf
308 !> call Printf(fmt='pattern= %c : text= %c : start= %d : length= %d', &
309 !> & c1=trim(pattern), c2=trim(text), i=(/start, length/))
310 !> end subroutine formatted_print
311 !>
312 !> end program regex_test
313 !>~~~~~~~~~~~~~~~
314 !> このプログラムを実行することで以下の出力が得られるはずです。
315 !>
316 !> pattern= -> : text= time->0.0,x->hoge : start= 5 : length= 2
317 !> pattern= ^##+ : text= ####### hoge : start= 1 : length= 7
318 !> pattern= @+$ : text= # hoge @@@ : start= 8 : length= 3
319 !>
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)