regex - RegEx - 字符串前的两个单词(正向前瞻)
问题描述
这些是示例字符串。我想在字符串“姓名、姓氏、签名”之后提取两个单词。我的第二部分是正确的,.*(Name, Surname, signature)
但我无法将结果限制为两个词。
例子:
小时数:4,Michael Keys姓名、姓氏、签名 固定式 4 Martha Johnson姓名、姓氏、签名
解决方案
利用
\p{Lu}\p{L}*\s+\p{Lu}\p{L}*(?=Name, Surname, signature)
见证明
解释
--------------------------------------------------------------------------------
\p{Lu} any uppercase letter
--------------------------------------------------------------------------------
\p{L}* any letter (0 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\p{Lu} any uppercase letter
--------------------------------------------------------------------------------
\p{L}* any letter (0 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
Name, Surname, 'Name, Surname, signature'
signature
--------------------------------------------------------------------------------
) end of look-ahead
推荐阅读
- elasticsearch - 如何根据elasticsearch中的父文档字段对嵌套聚合字段进行排序?
- python - VSCode 远程 SSH:在新终端中运行 Python 文件
- php - JSON 正文末尾出现意外数字
- android - Kotlin - 如何知道常量对象中是否存在字符串?
- python - 如何在 Bokeh (Python) 中编写自定义 JS 回调?
- .net - 接口函数应该强制输入吗?
- c++ - 仅在第一次实际写入时覆盖文件
- docker - Docker Build 在控制台输出中缺少中间层
- javascript - 打字稿:异步等待 promise.all 循环所有结果的问题
- java - Spring MVC验证控制器中POST / PUT方法中的所有参数