2

我有以下Json:

{"field1": "someText",
  "field2": "Text Again",
  "field3": "Text Again"}

我需要匹配以大写字母开头的任何短语的第一次出现(例如“Text Again”)

我写了以下内容:

("[A-Za-z]+\s[A-Za-z]+")

例如,在使用https://regex101.com/进行测试时,它确实可以正常工作。但是,它似乎不能作为ReplaceTextWithMapping (Apache NiFi) 使用的一部分正确发挥作用。正则表达式不正确吗?

谢谢您的帮助

4

2 回答 2

2

描述

:\s*"\s*(?=[A-Z])(?![^"]*?\s[a-z])([A-Za-z\s]+)"

正则表达式可视化

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

  • 在似乎是 JSON 编码字符串的值侧找到第一个标题大小写字符串
  • 确保每个单词都大写
  • 返回引号内的值作为捕获组 1

例子

现场演示

https://regex101.com/r/eO0xW6/1

源字符串

{"field1": "someText",
  "field2": "Text again",
  "field3": "Text Again"}

第一场比赛

Text Again

解释

概括

  • :\s*"验证仅检查 JSON 的值侧
  • \s*匹配开引号后的任何空格(如果存在)
  • (?=[A-Z])确保字符串中的第一个字符是大写的
  • (?![^"]*?\s[a-z])查找后跟小写字符的任何空格。如果找到,那么这不是匹配项
  • ([A-Za-z\s]+)捕获引号内的所有字符
  • "匹配报价

详细的

NODE                     EXPLANATION
----------------------------------------------------------------------
  :                        ':'
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  "                        '"'
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    [A-Z]                    any character of: 'A' to 'Z'
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    [^"]*?                   any character except: '"' (0 or more
                             times (matching the least amount
                             possible))
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
    [a-z]                    any character of: 'a' to 'z'
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [A-Za-z\s]+              any character of: 'A' to 'Z', 'a' to
                             'z', whitespace (\n, \r, \t, \f, and "
                             ") (1 or more times (matching the most
                             amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  "                        '"'
----------------------------------------------------------------------
于 2016-05-21T02:55:44.407 回答
1

我已将我对这个问题的发现发布到 Apache NiFi 邮件列表:

http://apache-nifi-developer-list.39713.n7.nabble.com/Issues-with-Regex-used-with-ReplaceTextWithMapping-where-am-I-going-wrong-tc10592.html

我没有收到社区的任何确认,但在我看来,虽然[A-Z][A-Za-z]*\s[A-Z][A-Za-z]*在这种情况下正则表达式是正确的,但处理器 (ReplaceTextWithMapping) 不能很好地处理空格 (\s) 并且字符串包含两个单词之间的空格.

于 2016-05-23T12:11:26.070 回答