从 C# 中的字符串中提取最后一个匹配项
我的字符串格式为 [abc].[some other string].[can.also.contain.periods].[our match]
我现在想要匹配字符串“our match”(即没有括号),所以我玩了环顾四周之类的东西。我现在得到了正确的匹配,但我认为这不是一个干净的解决方案。
(?<=\.?\[) starts with '[' or '.['
([^\[]*) our match, i couldn't find a way to not use a negated character group
`.*?` non-greedy did not work as expected with lookarounds,
it would still match from the first match
(matches might contain escaped brackets)
(?=\]$) string ends with an ]
语言是.net/c#。如果有一个不涉及正则表达式的更简单的解决方案,我也很高兴知道
真正让我恼火的是我无法使用 (.*?)
来捕获字符串,因为看起来非贪婪不适用于lookbehinds。
我也尝试过: Regex.Split(str, @"\]\.\[").Last().TrimEnd(']');
,但我对这个解决方案并不感到自豪任何一个
i have strings in the form [abc].[some other string].[can.also.contain.periods].[our match]
i now want to match the string "our match" (i.e. without the brackets), so i played around with lookarounds and whatnot. i now get the correct match, but i don't think this is a clean solution.
(?<=\.?\[) starts with '[' or '.['
([^\[]*) our match, i couldn't find a way to not use a negated character group
`.*?` non-greedy did not work as expected with lookarounds,
it would still match from the first match
(matches might contain escaped brackets)
(?=\]$) string ends with an ]
language is .net/c#. if there is an easier solution not involving a regex i'd be also happy to know
what really irritates me is the fact, that i cannot use (.*?)
to capture the string, as it seems non-greedy does not work with lookbehinds.
i also tried: Regex.Split(str, @"\]\.\[").Last().TrimEnd(']');
, but i'm not really pround of this solution either
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下应该可以解决问题。假设字符串在最后一个匹配之后结束。
The following should do the trick. Assuming the string ends after the last match.
假设您可以保证输入格式,并且它只是您想要的最后一个条目,则可以使用
LastIndexOf
:Assuming you can guarantee the input format, and it's just the last entry you want,
LastIndexOf
could be used:使用 String.Split():
您在 splitted[7] 中得到“不匹配”,并且 can.also.contain.periods 保留为一个字符串(splitted[4])
编辑:数组将在 [] 内包含字符串,然后。等等,所以如果您有可变数量的组,您可以使用它来获取您想要的值(或删除只是“。”的字符串)
编辑以将反斜杠添加到分隔符以处理诸如“\”之类的情况[abc\]'
Edit2:对于嵌套的 []:
你在最后一个元素(索引 3)中匹配],并且你必须删除额外的 ]
With String.Split():
you get "out match" in splitted[7] and can.also.contain.periods is left as one string (splitted[4])
Edit: the array will have the string inside [] and then . and so on, so if you have a variable number of groups, you can use that to get the value you want (or remove the strings that are just '.')
Edited to add the backslash to the separator to treat cases like '\[abc\]'
Edit2: for nested []:
you our [the] match] in the last element (index 3) and you'd have to remove the extra ]
您有多种选择:
RegexOptions.RightToLeft
- 是的,.NET 正则表达式可以做到这一点!使用它!pattern
会变成.*(pattern)
.*\[([^\]]*)\]
,然后提取\1
捕获的内容 (在 rubular.com 上查看此内容)参考文献
You have several options:
RegexOptions.RightToLeft
- yes, .NET regex can do this! Use it!pattern
becomes.*(pattern)
.*\[([^\]]*)\]
, then extract what\1
captures (see this on rubular.com)References