Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在正则表达式中从搜索中排除某些单词?_Regex - Fatal编程技术网

Regex 如何在正则表达式中从搜索中排除某些单词?

Regex 如何在正则表达式中从搜索中排除某些单词?,regex,Regex,我试着做一些算术表达式的语法检查。 所以,否定测试之一是:变量(或常量)和左大括号之间没有运算符。诸如此类: if (expression.matches(".*[a-zA-Z|0-9]+\\(.*")) { throw new IllegalArgumentException("Syntax error in the expression: " + expression); } 但是我需要在以下格式的表达

我试着做一些算术表达式的语法检查。 所以,否定测试之一是:变量(或常量)和左大括号之间没有运算符。诸如此类:

    if (expression.matches(".*[a-zA-Z|0-9]+\\(.*")) {
            throw new IllegalArgumentException("Syntax error in the expression: " + expression);
        }
但是我需要在以下格式的表达式中使用函数(如sin、cos等):sin(number),所以测试不起作用,因为上面的规则说某些运算符 需要在“sin”和“(”之间

是否可以在上述规则中添加使用sin、cos、tan、cot等的例外情况。? 我的意思是类似于(^sin | cos | tan | cot) 或者我需要编辑正则表达式中的语法规则吗?

您可以使用


if(expression.matches(“%a-zA-Z0-9”)(?你的意思是如果字符串包含
sin
cos
tan
ot
cot
,比如
”(?!\\b(?:co[ts]| tan | sin)\\b)。*[a-zA-Z-0-9]+\\(
)。是的。但是你的规则不起作用(我只是在cos(1+2)+sin 1+2)上试过了)它不起作用是因为…不要只说“它不起作用”,因为它起作用了,但很可能不是你期望的方式。你期望什么?展示一些测试用例。-cos(1+2)+sin(a)-var*(3+2)=正确(必须不匹配)const(1+2)+const123(a)-tan*(3+2)=不正确(不匹配)Try
。*[a-zA-Z0-9](?