M_match Module

NAME

M_match(3fp) - [M_MATCH] Basic Regular Expressions
(LICENSE:PD)

SYNOPSIS

use M_match, only: match, amatch, getpat, makpat
use M_match, only: YES, MAXPAT, MAXARG, MAXLINE, EOS, NEWLINE, ERR

DESCRIPTION

Find a string matching a regular expression.

   *   zero or more occurrences of the previous character
   .   any character
   ^   beginning of line
   $   end of line
   []  class of characters. Inside the braces

        ^  at the beginning of the class means to
           negate the class.
        -  if not the first or last character in
           the class, denotes a range of characters
   Escape characters:
    \\n  newline
    \\r  carriage return
    \\t  tab
    \\b  backspace

EXAMPLE

AUTHOR

John S. Urban

REFERENCE

“Software Tools” by Kernighan and Plauger , 1976

LICENSE

Public Domain



Contents


Variables

Type Visibility Attributes Name Initial
integer(kind=byte), public, parameter :: BLANK = 32_byte
integer(kind=byte), public, parameter :: EOS = 10002_byte
integer(kind=byte), public, parameter :: ERR = 10001_byte
integer(kind=byte), public, parameter :: MAXARG = 512
integer(kind=byte), public, parameter :: MAXLINE = 1024_byte
integer(kind=byte), public, parameter :: MAXPAT = 512
integer, public, parameter :: MAXTAGS = 10
integer(kind=byte), public, parameter :: NO = 0_byte
integer(kind=byte), public, parameter :: YES = 1_byte
integer(kind=byte), public :: bpos(maxsubs)
integer(kind=byte), public :: epos(maxsubs)

Interfaces

public interface amatch

  • private function amatch_(lin, from, pat)

    NAME

    amatch(3f) - [M_MATCH] look for pattern matching regular expression; returns its location
    (LICENSE:PD)
    

    SYNOPSIS

    loc = amatch(line, from, pat, tagbeg, tagend)
    
     character(len=*),intent(in) :: line
     integer,intent(in)          :: from
     character                   :: pat(MAXPAT)
     integer                     :: tagbeg(MAXTAGS), tagend(MAXTAGS)
     integer                     :: loc
    

    DESCRIPTION

    AMATCH scans LINE starting at location FROM, looking
    for a pattern which matches the regular expression coded
    in PAT. If the pattern is found, its starting location
    in LINE is returned. If the pattern is not found, AMATCH
    returns 0.
    
    The regular expression in PAT must have been previously
    encoded by GETPAT(3f) or MAKPAT(3f). (For a complete description
    of regular expressions, see the manpage for M_match.)
    
    AMATCH(3f) is a special-purpose version of MATCH(3f), which should
    be used in most cases.
    

    OPTIONS

    LINE           input line to scan
    FROM           beginning location to start scan from
    PAT            coded regular expression encoded by GETPAT(3f) or MAKPAT(3f)
    TAGBEG,TAGEND  element "i + 1" returns start or end, respectively, of "i"th tagged subpattern
    

    RETURNS

    LOC   returns location match was found or zero (0) if no match remains
    

    EXAMPLE

    Sample program:

     program demo_amatch
     use :: M_match, only : getpat, amatch
     use :: M_match, only : MAXPAT, MAXARG, MAXLINE, MAXTAGS, YES, ERR
     implicit none
     ! find _ find patterns in text
     integer                      :: pat(MAXPAT)
     character(len=MAXARG-1)      :: argument
     integer                      :: stat
     integer                      :: ios
     integer                      :: len_arg
     integer                      :: loc
     integer                      :: ii
     character(len=MAXLINE-2)     :: line
     integer                      :: tagbeg(MAXTAGS),tagend(MAXTAGS)
     call get_command_argument(1, argument,status=stat,length=len_arg)
     if(stat.ne.0.or.argument.eq.'')then
        write(*,*)"usage: find pattern."
     elseif(getpat(argument(:len_arg), pat) .eq. ERR) then
        write(*,*)"illegal pattern."
     else
        INFINITE: do
           read(*,'(a)',iostat=ios)line
           tagbeg=-9999;tagend=-9999
           if(ios.ne.0)exit
           loc = amatch(trim(line), 1, pat, tagbeg, tagend) ! returns location/0
           if(loc.gt.0)then ! matched; if no match, loc is returned as 0
              write(*,'(*(a))')trim(line)
              ! (element "i + 1" returns start or end, respectively, of "i"th tagged subpattern)
              write(*,'(*(i0,1x,i0,1x,i0,/))')(ii,tagbeg(ii),tagend(ii),ii=1,size(tagbeg))
           endif
        enddo INFINITE
     endif
     end program demo_amatch
    

    SEE ALSO

    match, getpat, makpat
    

    DIAGNOSTICS

    None
    

    AUTHOR

    John S. Urban
    

    REFERENCE

    "Software Tools" by Kernighan and Plauger , 1976
    

    LICENSE

    Public Domain
    

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=chr) :: lin(maxline)
    integer(kind=def) :: from
    integer(kind=chr) :: pat(maxpat)

    Return Value integer(kind=def)

  • private function amatch__(lin_str, from, pat)

    Arguments

    Type IntentOptional Attributes Name
    character(len=*), intent(in) :: lin_str
    integer, intent(in) :: from
    integer(kind=chr) :: pat(maxpat)

    Return Value integer(kind=def)

public interface getpat

  • private function getpat_(arg, pat)

    NAME

    getpat(3f) - [M_MATCH] convert str into pattern
    (LICENSE:PD)
    

    SYNOPSIS

    integer function getpat(str, pat)

    DESCRIPTION

    convert str into pattern
    

    OPTIONS

    EXAMPLE

    AUTHOR

    John S. Urban

    REFERENCE

    “Software Tools” by Kernighan and Plauger , 1976

    LICENSE

    Public Domain

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=def) :: arg(maxarg)
    integer(kind=def) :: pat(maxpat)

    Return Value integer(kind=def)

  • private function getpat__(arg_str, pat)

    Arguments

    Type IntentOptional Attributes Name
    character(len=*), intent(in) :: arg_str
    integer(kind=def), intent(out) :: pat(maxpat)

    Return Value integer(kind=def)

public interface makpat

  • private function makpat_(arg, from, delim, pat)

    NAME

    makpat(3f) - [M_MATCH] make pattern from arg(from), terminate on delim
    (LICENSE:PD)
    

    SYNOPSIS

    integer function makpat(arg, from, delim, pat)

    DESCRIPTION

    make pattern from arg(from), terminate on delim

    OPTIONS

    EXAMPLE

    AUTHOR

    John S. Urban

    REFERENCE

    “Software Tools” by Kernighan and Plauger , 1976

    LICENSE

    Public Domain

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=chr) :: arg(maxarg)
    integer(kind=def) :: from
    integer(kind=chr) :: delim
    integer(kind=chr) :: pat(maxpat)

    Return Value integer(kind=def)

public interface match

  • private function match_(lin, pat)

    NAME

    match(3f) - [M_MATCH] find match to a basic regular expression anywhere on input string
    (LICENSE:PD)
    

    SYNOPSIS

    integer function match(line, pattern)
    
     character(len=*),intent(in) :: line
     integer,intent(in)          :: pattern(MAXPAT)
    

    DESCRIPTION

    Given a BRE(Basic Regular Expression) converted to a pattern return whether an input string matches it.

    OPTIONS

    LIN  string to search for a match to the pattern
    PAT  pattern generated from a BRE using getpat(3f) or makpat(3f).
    

    EXAMPLE

    Sample program:
    
     program demo_match
     use :: M_match, only : getpat, match
     use :: M_match, only : MAXPAT, MAXARG, MAXLINE, YES, ERR
     implicit none
     ! find _ find patterns in text
     integer                      :: pat(MAXPAT)
     character(len=MAXARG-1)      :: argument
     integer                      :: stat
     integer                      :: ios
     integer                      :: len_arg
     character(len=MAXLINE-2)     :: line
     call get_command_argument(1, argument,status=stat,length=len_arg)
     if(stat.ne.0.or.argument.eq.'')then
        write(*,*)"usage: find pattern."
     elseif(getpat(argument(:len_arg), pat) .eq. ERR) then
        write(*,*)"illegal pattern."
     else
        INFINITE: do
           read(*,'(a)',iostat=ios)line
           if(ios.ne.0)exit
           if(match(trim(line), pat) .eq. YES) then
              write(*,'(*(a))')trim(line)
           endif
        enddo INFINITE
     endif
     end program demo_match
    

    AUTHOR

    John S. Urban

    REFERENCE

    “Software Tools” by Kernighan and Plauger , 1976

    LICENSE

    Public Domain

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=chr) :: lin(maxline)
    integer(kind=chr) :: pat(maxpat)

    Return Value integer(kind=def)

  • private function match__(lin_str, pat)

    Arguments

    Type IntentOptional Attributes Name
    character(len=*), intent(in) :: lin_str
    integer(kind=chr) :: pat(maxpat)

    Return Value integer(kind=def)


Derived Types

type, public ::  regex_pattern

Components

Type Visibility Attributes Name Initial
integer, public :: pat(MAXPAT)