c# - 找不到此模式的正则表达式
问题描述
对于 dotnet 应用程序,我有一个字符串模式,如下所示:
TEAM AND CO TEAM
TESTER
Name anderson rahul alice
我只需要捕获“Name”的值,但是只有当字符串序列“TEAM AND CO TEAM”在一行中并且“TESTER”在下一行并且“Name”出现时才会捕获name的值在下一行。
下面是我的正则表达式,但它也选择了第二次出现的名称,我不需要:
\bName\s+(.*)$
输入字符串:
TEAM AND CO TEAM
TESTER
Name anderson rahul alice
LTest SYNC DIM TESTER
PHYSICS
Name KIM SID andy
任何帮助都将是可观的。
解决方案
利用
(?m)(?<=^TEAM AND CO TEAM\r?\nTESTER\r?\nName\s).+
请参阅正则表达式证明。
解释
--------------------------------------------------------------------------------
(?m) set flags for this block (with ^ and $
matching start and end of line) (case-
sensitive) (with . not matching \n)
(matching whitespace and # normally)
--------------------------------------------------------------------------------
(?<= look behind to see if there is:
--------------------------------------------------------------------------------
^ the beginning of a "line"
--------------------------------------------------------------------------------
TEAM AND CO TEAM 'TEAM AND CO TEAM'
--------------------------------------------------------------------------------
\r? '\r' (carriage return) (optional
(matching the most amount possible))
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
TESTER 'TESTER'
--------------------------------------------------------------------------------
\r? '\r' (carriage return) (optional
(matching the most amount possible))
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
Name 'Name'
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
.+ any character except \n (1 or more times
(matching the most amount possible))
推荐阅读
- powershell - 如何使用 Powershell 将文件夹和文件从 RDP 服务器复制到本地计算机
- excel - 在 Excel 中返回 Null
- bash - 如何遍历字符串中的字符并仍然在 Bash 中检测空字符
- workflow-foundation-4 - 从 ModelItem 获取 WorkflowDesigner
- elasticsearch - ES - 基于存储桶属性中的值而不是文档值的子存储桶
- unity3d - unity:不同屏幕分辨率下按钮间距不同
- ruby-on-rails - 如何在 Twilio 视频会议中启用录制?
- javascript - 当我更改其中一个时,为什么所有日期选择器值都变为空?
- matlab - 有符号平方根 SSR
- ruby - 验证失败:使用 rspec 上传徽标时,徽标不能为空