regexec(3f) - [M_regex] Execute a compiled regex against a string
Synopsis
Description
Options
Returns
Examples
function regexec(this,string,matches,flags,status) result(match)
logical :: match ! .TRUE. if the pattern matched type(regex_type),intent(in) :: this character(len=*),intent(in) :: string integer, intent(out),optional :: matches(:,:) character(len=*),intent(in), optional :: flags integer, intent(out),optional :: status
THIS regex object STRING target string MATCHES match locations dimension(2,*) FLAGS flag characters for partial lines:
o b = no beginning-of-line (REG_NOTBOL) o e = no end-of-line (REG_NOTEOL) STATUS if absent, errors are fatal
0 successfully found match 1 successfully found no match other regexec(3f) failed
regexec LOGICAL value is .TRUE. if a match was found
Sample program
program demo_regexec ! read regular expression from command line and look for it in lines read from stdin. use M_regex, only: regex_type, regcomp, regexec, regfree implicit none integer :: command_argument_length character(len=:),allocatable :: command_argument character(len=1024) :: input_line type(regex_type) :: regex logical :: match integer :: ios call get_command_argument(number=1,length=command_argument_length) allocate(character(len=command_argument_length) :: command_argument) call get_command_argument(1, command_argument) call regcomp(regex,command_argument,xn) ! compile up regular expression INFINITE: do read(*,(a),iostat=ios)input_line if(ios.ne.0)exit INFINITE match=regexec(regex,input_line) ! look for a match in (remaining) string if(.not.match)cycle INFINITE ! if no match found go for next line write(*,(a)) trim(input_line) ! show line with match enddo INFINITE call regfree(regex) ! free memory used for compiled regular expression end program demo_regexecSample Output
demo_regexec lt;integergt; < demo_regexec.f90 integer :: command_argument_length integer :: ios
Nemo Release 3.1 | regexec (3) | February 23, 2025 |