regexshellposix

test for regex in string with a posix shell


how can I test if a string matches a particular string matches regex with a basic (no bash or anything) posix shell script (in an if statement)?


Solution

  • You can use expr command to evaluate regular expression in a POSIX shell:

    s='Abc'
    expr $s : '^[[:alpha:]]\+'
    
    3