我想检查一个字符串是否包含一组条件:
体育运动
let myvar = '.....'
if myvar doesn't contain:
- 'cos'
- or 'sin'
- or 'tan'
- or 'log'
- or only contains of this: [0-9*/:-+<space>]
- or ... etc
give an error message.
我想将所有条件放在一个列表中并检查字符串是否
具有以下条件之一。
如果字符串不具备这些条件之一,则给出错误消息。
谁能帮我找到正确的正则表达式?
最佳答案
我建议使用match()
函数
function! Test(s)
if match(a:s, '\<\(cos\|sin\|tan\|log\)\>') < 0
echo "functions not matched"
endif
if match(a:s, '^[0-9*\\\/:+\- ]*$') >= 0
echo "only numerics and operators"
endif
endf
希望这个使用示例有所帮助。测试一下
:call Test('123 * 38 - 04 : 7 + 1269')
:call Test('biological')
:call Test('log(x)')
关于regex - 如何检查字符串是否包含一组条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10478975/