为了账号安全,请及时绑定邮箱和手机立即绑定

有没有办法从正则表达式中匹配和删除逻辑运算符

有没有办法从正则表达式中匹配和删除逻辑运算符

缥缈止盈 2023-09-21 14:17:06
我不想在正则表达式中使用 Match 函数时包含逻辑运算符(and、or、not)。我在下面尝试过,但它没有按预期工作。有人可以帮帮我吗。我用于解析的字符串类型:示例:1. Input -->'(Value1==6) and (Value2==0)?1:0'Output --> ["Value1", "Value2"]2. Input : 'Value_1'Output -->["Value_1"]3. Input : '(Value_1 * Value_2)'Output : ["Value1", "Value2"]4. Input : 'Value_Machine_Outcome==4?1:0'Output : Value_Machine_Outcome嵌套条件:无条件是否始终在括号中:否, 我将在下一步中使用Math.evaluate评估它们请举例如下:const paragraph = '(Value1==6) and (Value2==0)?1:0';const regex = /([a-zA-Z]+[a-zA-Z0-9_]+)(?<!and|or|not)/g;const found = paragraph.match(regex);console.log(found);// expected output: Array ["Value1", "Value2"]
查看完整描述

2 回答

?
SMILET

TA贡献1796条经验 获得超4个赞

试试这个正则表达式:([a-zA-Z]+[a-zA-Z0-9_]+)(?<![and|or|not]). 我刚刚更新了您的代码中的正则表达式,请测试并让我知道您是否有任何疑问。


const paragraph = '(Value1==6) and (Value2==0)?1:0';

const regex = /([a-zA-Z]+[a-zA-Z0-9_]+)(?<![and|or|not])/g;

const found = paragraph.match(regex);


console.log(found);


查看完整回答
反对 回复 2023-09-21
?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

您更新的问题完全改变了输入的性质。如果输入如此不同,您将需要匹配任何不以 、 或 以外的数字开头的“单词” andor但这not符合您最初的尝试,所以我想这是有道理的) :

const regex = /(?!and|or|not)\b[A-Z]\w*/gi;

实例:

const tests = [

    {

        str: "(Value1==6) and or not (Value2==0)?1:0",

        expect: ["Value1", "Value2"]

    },

    {

        str: "Value_1",

        expect: ["Value_1"]

    },

    {

        str: "(Value_1 * Value_2)",

        expect: ["Value_1", "Value_2"]

    },

    {

        str: "Value_Machine_Outcome==4?1:0",

        expect: ["Value_Machine_Outcome"] // Note I put this in an array

    }

];


const regex = /(?!and|or|not)\b[A-Z]\w*/gi;

for (const {str, expect} of tests) {

    const result = str.match(regex);

    const good = result.length === expect.length && result.every((v, i) => v === expect[i]);

    console.log(JSON.stringify(result), good ? "Ok" : "<== ERROR");

}

其工作原理是不允许andor、 和not,并要求在单词边界 ( \b) 处进行匹配。请注意,在测试中,我将Value_Machine_Outcome==4?1:0字符串的预期结果更改为数组,而不仅仅是字符串,就像所有其他结果一样。


问题完全改变输入之前的原始答案:

如果你想使用String.prototype.match,你可以对 a 使用正向后视(自 ES2018 起)(并匹配 a 之前的所有内容=

const regex = /(?<=\()[^=]+/g;

实例:

const paragraph = '(Value1==6) and (Value2==0)?1:0';

const regex = /(?<=\()[^=]+/g;

const found = paragraph.match(regex);


console.log(found);


// expected output: Array ["Value1", "Value2"]

如果您同意循环,则可以通过使用捕获组来避免后向查找(因为它们仅在 ES2018 中添加):


const regex = /\(([^=]+)/g;

const found = [];

let match;

while (!!(match = regex.exec(paragraph))) {

    found.push(match[1]);

}

实例:

const paragraph = '(Value1==6) and (Value2==0)?1:0';

const regex = /\(([^=]+)/g;

const found = [];

let match;

while (!!(match = regex.exec(paragraph))) {

    found.push(match[1]);

}


console.log(found);


// expected output: Array ["Value1", "Value2"]

在评论中你问:

我的表达式也可以包含下划线。就像它可以是 value_1、value_2 一样。那里能行得通吗?

我说会是因为上面的两个都匹配除了=.

后来你说:

当我的结构包含“Value_1”时它会忽略

同样,以上两者都可以与Value_1和配合使用Value_2

第一的:

const paragraph = '(Value_1==6) and (Value_2==0)?1:0';

const regex = /(?<=\()[^=]+/g;

const found = paragraph.match(regex);


console.log(found);


// expected output: Array ["Value1", "Value2"]

第二:

const paragraph = '(Value_1==6) and (Value_2==0)?1:0';

const regex = /\(([^=]+)/g;

const found = [];

let match;

while (!!(match = regex.exec(paragraph))) {

    found.push(match[1]);

}


console.log(found);


// expected output: Array ["Value1", "Value2"]


查看完整回答
反对 回复 2023-09-21
  • 2 回答
  • 0 关注
  • 64 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信