[英]Regex to only match a string that does not end with “)”
I have a plist file that has nested properties. 我有一个具有嵌套属性的plist文件。 I need to get the text immediately after the open "(", this is the "Name". I can do this fine, However, because there are nested properties, when my C# logic iterates over the lines in the plist, the name gets overwritten by the next property. Is there a way to only match an exact pattern; specifically do not match any part of a string ends with a ")"
我需要在打开“(”之后立即获取文本,这是“名称”。我可以做到这一点,但是,由于存在嵌套属性,因此当我的C#逻辑遍历plist中的行时,名称将有没有办法只匹配一个确切的模式;特别是不匹配以“)”结尾的字符串的任何部分
: (MyNameHere //Match
: (propertyone) // do not match, because it ends with ")"
here is the Regex I am using to match the name. 这是我用来匹配名称的Regex。
:\s\(([a-z,A-Z,0-9,-_]+)
I am using C#.net 4.5 我正在使用C#.net 4.5
Thanks 谢谢
I don't know what you are exactly trying to do but I suspect that balancing groups will interest you: https://msdn.microsoft.com/en-us/library/bs2twtah%28v=vs.110%29.aspx 我不知道您到底要做什么,但我怀疑平衡组会让您感兴趣: https ://msdn.microsoft.com/zh-cn/library/bs2twtah%28v=vs.110%29.aspx
If you want to match a line that doesn't end with and not contain a closing parenthesis, you can use this pattern: 如果要匹配不以结尾且不包含右括号的行,则可以使用以下模式:
(?>[^\n)]*)(?!\))
details: 细节:
(?>...) # is an atomic group that prevents the regex engine to backtrack
(?!\)) # is a negative lookahead (not followed by ...) to check there
# is not parenthesis after
The important thing is that *
(as all other quantifiers) is greedy by default. 重要的是
*
(和所有其他量词一样)默认情况下是贪婪的。
If you want to allow lines that can contain a closing parenthesis but can't end with a closing parenthesis: 如果要允许包含闭合括号但不能以闭合括号结尾的行:
(?m)^.*(?<!\))$
You need to add a positive lookahead assertion at the last if you don't want any other space character following [\\w-]+
. 如果您不想在
[\\w-]+
之后使用任何其他空格字符,则需要在最后添加一个肯定的超前断言。 (?=\\s|$)
positive lookahead which asserts that the match must be followed by a space character or end of the line anchor. (?=\\s|$)
正向超前,表示必须在匹配项后跟空格字符或行锚的结尾。
@":\s\(([\w-]+)(?=\s|$)"
Use \\s
if necessary or otherwise @":\\s\\(([\\w-]+)$
would be enough. 如有必要,请使用
\\s
,否则使用@":\\s\\(([\\w-]+)$
就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.