【问题标题】:Regex help needed: pattern almost works需要正则表达式帮助:模式几乎可以工作
【发布时间】:2021-01-18 16:16:14
【问题描述】:

我需要一些有关 ECMAScript 正则表达式的帮助。我目前使用的正则表达式几乎可以按要求工作,但有一个小问题。我需要匹配以下内容:

STATSTATS 不分大小写。此外,后面可能还有符号和数字。

示例: stats:3-2 是匹配项。 stats:5 是匹配项。 stats-4 是部分匹配,但 '-4' 应该被忽略。

如前所述,我正在使用的当前正则表达式几乎可以正常工作,如下所示: /STAT[S]*(?:(?:[\:](?<method>(\d)))(?:[\-](?<count>(\d)+))*)*/ig

此模式使用 regex101,实际上匹配所有条件并忽略以下示例中的 -4:stats-4,同时匹配单词“stats”。

但是,当我尝试在我正在编辑的插件中使用此模式时,就会出现问题。它目前只匹配statstatsstat:2,但不匹配stat:3-2stat-4(应该匹配'stat'但忽略'-4')。

我知道模式可能有点混乱,但我不擅长创建正则表达式。

具体用法(在 atom rpg-dice 插件中):

    roll() {
        const editor = atom.workspace.getActiveTextEditor();
        const regex = [
            /(\+|\-){1}([\d]+)/i,
            /([\d]+)d([\d]+)(?:([\+|\-]){1}([\d]+))*/i,
            /STAT[S]*(?:(?:[\:](?<method>(\d)))?(?:[\-](?<count>(\d)+))*)*/i
        ];

        if (editor) {
            // attempt to select the dice roll
            let selection = editor.getSelectedText();
            // if the selection failed, try selection another way.
            if (selection.length < 1) {
                editor.selectWordsContainingCursors();
                atom.commands.dispatch(atom.views.getView(editor), 'bracket-matcher:select-inside-brackets');
                selection = editor.getSelectedText();
            }
            // increase size of selection by 1, both left and right. (selects brackets)
            let range = editor.getSelectedBufferRange();
            let startColumn = range.start.column -1;
            let endColumn = range.end.column +1;
            editor.setSelectedBufferRange([[range.start.row, startColumn],[range.end.row, endColumn]]);
            // trim any whitespace from the selection
            selection.trim();

            /*
                regex pattern matching to determine the
                type of roll.
             */
             if (matches = selection.match(regex[0])) {                          // 1d20 roll; attack and ability checks
                type = 'check';
             } else if (matches = selection.match(regex[1])) {                   // typically a damage dice roll
                type = 'dmg';
             } else if (matches = selection.match(regex[2])) {                   // used for stat generation
                console.log(matches);
             } else {
                console.log('Cannot determine a suitable use.');
             }
     

【问题讨论】:

  • /STATS?(?::(?&lt;method&gt;\d)(?:-(?&lt;count&gt;\d+))?)?/gi should work 如果插件支持最新的 ECMAScript 规范。
  • 我刚试过,还是不行。基本上,如果我添加 - 后跟一个数字,它就不起作用。
  • @JLDNAdmin 因为你的第一个 \d 前面没有可选的 -,就像第二个一样。
  • @r3wt 你能告诉我吗?我没有关注。
  • 试试这个:/STAT[S]*(?:(?:[\:](?&lt;method&gt;(\d)))?(?:[\-](?&lt;count&gt;(\d)+))*)*/ig

标签: javascript regex


【解决方案1】:

你可以使用

/STATS?(?::(?<method>\d)(?:-(?<count>\d+))?)?/gi

请参阅regex demo详情

  • STATS? - STATSSTAT
  • (?::(?&lt;method&gt;\d)(?:-(?&lt;count&gt;\d+))?)? - 可选出现
    • : - 冒号
    • (?&lt;method&gt;\d) - 组“方法”:一位数
    • (?:-(?&lt;count&gt;\d+))? - 可选出现
      • - - 一个连字符
      • (?&lt;count&gt;\d+) - 组“计数”:一位或多位数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-30
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    相关资源
    最近更新 更多