描述
:\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
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------