[英]What is wrong with this regular expression?
I have created one regular expression which is supposed to match for line starting with Project followed by either # or : or - followed by 1 to 3 digit number or simply Title. 我创建了一个正则表达式,该表达式应该与以Project开头的行匹配,后跟#或:或-后跟1至3位数字或标题。 For example, following lines should get matched 例如,以下几行应匹配
Project # 1 项目1
Project#1 项目#1
Project :1 项目1
Project-123 项目-123
Project Title 项目名称
but following should not match 但是以下内容不匹配
Project ABCD ABCD项目
Project*978 项目* 978
My Project 我的项目
Projects handled 处理的项目
My regular expression is as follows : 我的正则表达式如下:
^(\s)*?((Project( )*?(#|:|-| )( )*?(\d){1,3})|(PROJECT( )*?(#|:|-| )( )*?(\d){1,3})|Project Title|PROJECT TITLE)\b
Project keyword should be at the start of the line. Project关键字应位于该行的开头。
For some text, this regular expression is working fine. 对于某些文本,此正则表达式可以正常工作。 But this regular expression is matching following line : 但是此正则表达式与以下行匹配:
Projects Handled: 处理的项目:
I have no clue why it is happening. 我不知道为什么会这样。 Can anyone find out what is wrong with my regular expression? 有人可以找出我的正则表达式有什么问题吗?
I am using C# to do this. 我正在使用C#来做到这一点。
Thanks in advance !!! 提前致谢 !!!
How about like this pattern? 这样的模式怎么样?
@"Project\s*[#: -]\s*(?:\d+|[A-Z][a-z]+)"
Its does not match to 它不符合
Project ABCD ABCD项目
Project*978 项目* 978
My Project 我的项目
Projects handled 处理的项目
But will match to following patterns 但将匹配以下模式
Project # 1
Project#1
Project :1
Project-123
Project Title
There is a confusion part in Project Title and Project ABCD though. 但是,“ 项目名称”和“ ABCD 项目”中有一个混乱的部分。 I assumed you only want title case. 我以为你只想要标题大小写。
This works for me: 这对我有用:
Regex project = new Regex(@"^\s*?(?:Project *[#:\- ] *(\d){1,3}|Project Title)",
RegexOptions.ECMAScript | RegexOptions.IgnoreCase | RegexOptions.Multiline);
^\s*Project\s*([\-#:]\s*\d{1,3}|Title)\b
这样可以在Project
之前和之后以及-#:
和三位数之间保留可选空格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.