需要正则表达式匹配一行中单词的反向

时间:2016-05-28 18:23:37

标签: regex

示例输入字符串:result = []; add = hour + hours - (length / 2) - (length % 2) + 1; for (i = 0; i < length; i++) { result[(add + i) % length] = (add + i) % hours; }

用户输入的字符串:function getColorIndexes(hour, hours, length) { var i, result, add; if (hours % length) throw "number of hours must be multiple of length"; result = []; add = hour + hours - (length / 2) - (length % 2) + 1; for (i = 0; i < length; i++) { result[(add + i) % length] = (add + i) % hours; } return result; } console.log ('hour=3: ' + getColorIndexes(3, 24, 12)); console.log ('hour=5: ' + getColorIndexes(5, 24, 12)); console.log ('hour=9: ' + getColorIndexes(9, 24, 12)); console.log ('hour=23: ' + getColorIndexes(23, 24, 12));

在上面的两个字符串中,我需要逐字解析输入字符串和User键入的字符串,并且需要在用户键入的字符串中找到匹配项。如果

  1. 输入字符串中的常规字符应与ralugeR匹配。
  2. 应与si
  3. 匹配
  4. 真的应该与yllear相匹配
  5. 应与ot
  6. 匹配

    注意:输入字符串不是常量字符串,它可能会有所不同。 例如:

    另一个示例字符串可以是:Regular Expression is really Tough to understand

    用户输入:ralugeR Expression si yllear Tough ot understand

    我正在寻找一个正则表达式来匹配行中单词的反向。但输入单词可能是可更改的。

    但输入字符串将包含大写和小写的任何字母,以及其中的数字。

1 个答案:

答案 0 :(得分:2)

Foreward

使用正则表达式并不适合这类工作。这里可以遇到很多边缘情况。

描述

\b([^\s])([^\s])(?:([^\s])(?:([^\s])(?:([^\s])(?:([^\s])(?:([^\s])(?:([^\s])(?:([^\s]))?)?)?)?)?)?)?\b(?=.*?\n.*?\b((?:(?:(?:(?:(?:(?:(?:\9)?\8)?\7)?\6)?\5)?\4)?\3)?\2\1)\b)

Regular expression visualization

注意:要更好地查看图像,请右键单击图像,然后在新窗口中选择视图。

摘要

此正则表达式将执行以下操作:

  • 假设输入字符串由以下两行由新行字符分隔组成
    • 输入字符串
    • 用户创建的字符串
  • 假设单词最多只能包含9个字符
  • 假设最小的单词长度为2个字符,因为后面拼写的a仍然是a但不是真的倒退。
  • 不验证或匹配单词位置,换句话说,向后拼写的单词可以出现在第二行的任何单词位置。
  • 将第一行中的单词与第二行进行比较,其中第二行中的单词必须向后拼写以匹配
  • 捕获组0将从第一行获取单词
  • 捕获组1-9将始终具有单独的字母
  • 捕获组10将从第二行获得向后的单词

实施例

现场演示

https://regex101.com/r/zO4yI1/4

示例文字

我不确定yllear是否在原始问题中故意拼错,但我在示例中添加了另外一个错误拼写的raelly

Regular Expression is really raelly Tough to understand
ralugeR Expression si yllear Tough ot understand

样本匹配

MATCH 1
1.  [0-1]   `R`
2.  [1-2]   `e`
3.  [2-3]   `g`
4.  [3-4]   `u`
5.  [4-5]   `l`
6.  [5-6]   `a`
7.  [6-7]   `r`
10. [56-63] `ralugeR`

MATCH 2
1.  [19-20] `i`
2.  [20-21] `s`
10. [75-77] `si`

MATCH 3
1.  [29-30] `r`
2.  [30-31] `a`
3.  [31-32] `e`
4.  [32-33] `l`
5.  [33-34] `l`
6.  [34-35] `y`
10. [78-84] `yllear`

MATCH 4
1.  [42-43] `t`
2.  [43-44] `o`
10. [91-93] `ot`

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [^\s]                    any character except: whitespace (\n,
                             \r, \t, \f, and " ")
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    [^\s]                    any character except: whitespace (\n,
                             \r, \t, \f, and " ")
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    (                        group and capture to \3:
----------------------------------------------------------------------
      [^\s]                    any character except: whitespace (\n,
                               \r, \t, \f, and " ")
----------------------------------------------------------------------
    )                        end of \3
----------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
----------------------------------------------------------------------
      (                        group and capture to \4:
----------------------------------------------------------------------
        [^\s]                    any character except: whitespace
                                 (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
      )                        end of \4
----------------------------------------------------------------------
      (?:                      group, but do not capture (optional
                               (matching the most amount possible)):
----------------------------------------------------------------------
        (                        group and capture to \5:
----------------------------------------------------------------------
          [^\s]                    any character except: whitespace
                                   (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
        )                        end of \5
----------------------------------------------------------------------
        (?:                      group, but do not capture (optional
                                 (matching the most amount
                                 possible)):
----------------------------------------------------------------------
          (                        group and capture to \6:
----------------------------------------------------------------------
            [^\s]                    any character except: whitespace
                                     (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
          )                        end of \6
----------------------------------------------------------------------
          (?:                      group, but do not capture
                                   (optional (matching the most
                                   amount possible)):
----------------------------------------------------------------------
            (                        group and capture to \7:
----------------------------------------------------------------------
              [^\s]                    any character except:
                                       whitespace (\n, \r, \t, \f,
                                       and " ")
----------------------------------------------------------------------
            )                        end of \7
----------------------------------------------------------------------
            (?:                      group, but do not capture
                                     (optional (matching the most
                                     amount possible)):
----------------------------------------------------------------------
              (                        group and capture to \8:
----------------------------------------------------------------------
                [^\s]                    any character except:
                                         whitespace (\n, \r, \t, \f,
                                         and " ")
----------------------------------------------------------------------
              )                        end of \8
----------------------------------------------------------------------
              (?:                      group, but do not capture
                                       (optional (matching the most
                                       amount possible)):
----------------------------------------------------------------------
                (                        group and capture to \9:
----------------------------------------------------------------------
                  [^\s                     any character except:
                  ]                        whitespace (\n, \r, \t,
                                           \f, and " ")
----------------------------------------------------------------------
                )                        end of \9
----------------------------------------------------------------------
              )?                       end of grouping
----------------------------------------------------------------------
            )?                       end of grouping
----------------------------------------------------------------------
          )?                       end of grouping
----------------------------------------------------------------------
        )?                       end of grouping
----------------------------------------------------------------------
      )?                       end of grouping
----------------------------------------------------------------------
    )?                       end of grouping
----------------------------------------------------------------------
  )?                       end of grouping
----------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    \n                       '\n' (newline)
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
----------------------------------------------------------------------
    (                        group and capture to \10:
----------------------------------------------------------------------
      (?:                      group, but do not capture (optional
                               (matching the most amount possible)):
----------------------------------------------------------------------
        (?:                      group, but do not capture (optional
                                 (matching the most amount
                                 possible)):
----------------------------------------------------------------------
          (?:                      group, but do not capture
                                   (optional (matching the most
                                   amount possible)):
----------------------------------------------------------------------
            (?:                      group, but do not capture
                                     (optional (matching the most
                                     amount possible)):
----------------------------------------------------------------------
              (?:                      group, but do not capture
                                       (optional (matching the most
                                       amount possible)):
----------------------------------------------------------------------
                (?:                      group, but do not capture
                                         (optional (matching the most
                                         amount possible)):
----------------------------------------------------------------------
                  (?:                      group, but do not capture
                                           (optional (matching the
                                           most amount possible)):
----------------------------------------------------------------------
                    \9                       what was matched by
                                             capture \9
----------------------------------------------------------------------
                  )?                       end of grouping
----------------------------------------------------------------------
                  \8                       what was matched by
                                           capture \8
----------------------------------------------------------------------
                )?                       end of grouping
----------------------------------------------------------------------
                \7                       what was matched by capture
                                         \7
----------------------------------------------------------------------
              )?                       end of grouping
----------------------------------------------------------------------
              \6                       what was matched by capture \6
----------------------------------------------------------------------
            )?                       end of grouping
----------------------------------------------------------------------
            \5                       what was matched by capture \5
----------------------------------------------------------------------
          )?                       end of grouping
----------------------------------------------------------------------
          \4                       what was matched by capture \4
----------------------------------------------------------------------
        )?                       end of grouping
----------------------------------------------------------------------
        \3                       what was matched by capture \3
----------------------------------------------------------------------
      )?                       end of grouping
----------------------------------------------------------------------
      \2                       what was matched by capture \2
----------------------------------------------------------------------
      \1                       what was matched by capture \1
----------------------------------------------------------------------
    )                        end of \10
----------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
相关问题