test for regex in string with a posix shell

纵然是瞬间 提交于 2020-03-18 05:54:25

问题


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


回答1:


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

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

3

expr returns # of matched characters which is 3 in this case.



来源:https://stackoverflow.com/questions/35693980/test-for-regex-in-string-with-a-posix-shell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!