从 C# 中的字符串中提取最后一个匹配项

发布于 2024-09-06 16:15:14 字数 768 浏览 4 评论 0原文

我的字符串格式为 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云雾 2024-09-13 16:15:14

以下应该可以解决问题。假设字符串在最后一个匹配之后结束。

string input = "[abc].[some other string].[can.also.contain.periods].[our match]";

var search = new Regex("\\.\\[(.*?)\\]$", RegexOptions.RightToLeft);

string ourMatch = search.Match(input).Groups[1]);

The following should do the trick. Assuming the string ends after the last match.

string input = "[abc].[some other string].[can.also.contain.periods].[our match]";

var search = new Regex("\\.\\[(.*?)\\]$", RegexOptions.RightToLeft);

string ourMatch = search.Match(input).Groups[1]);
在巴黎塔顶看东京樱花 2024-09-13 16:15:14

假设您可以保证输入格式,并且它只是您想要的最后一个条目,则可以使用 LastIndexOf

string input = "[abc].[some other string].[can.also.contain.periods].[our match]";

int lastBracket = input.LastIndexOf("[");
string result = input.Substring(lastBracket + 1, input.Length - lastBracket - 2);

Assuming you can guarantee the input format, and it's just the last entry you want, LastIndexOf could be used:

string input = "[abc].[some other string].[can.also.contain.periods].[our match]";

int lastBracket = input.LastIndexOf("[");
string result = input.Substring(lastBracket + 1, input.Length - lastBracket - 2);
小瓶盖 2024-09-13 16:15:14

使用 String.Split():

string input = "[abc].[some other string].[can.also.contain.periods].[our match]";
char[] seps = {'[',']','\\'};
string[] splitted = input.Split(seps,StringSplitOptions.RemoveEmptyEntries);

您在 splitted[7] 中得到“不匹配”,并且 can.also.contain.periods 保留为一个字符串(splitted[4])

编辑:数组将在 [] 内包含字符串,然后。等等,所以如果您有可变数量的组,您可以使用它来获取您想要的值(或删除只是“。”的字符串)

编辑以将反斜杠添加到分隔符以处理诸如“\”之类的情况[abc\]'

Edit2:对于嵌套的 []:

string input = @"[abc].[some other string].[can.also.contain.periods].[our [the] match]";
string[] seps2 = { "].["};
string[] splitted = input.Split(seps2, StringSplitOptions.RemoveEmptyEntries);

你在最后一个元素(索引 3)中匹配],并且你必须删除额外的 ]

With String.Split():

string input = "[abc].[some other string].[can.also.contain.periods].[our match]";
char[] seps = {'[',']','\\'};
string[] splitted = input.Split(seps,StringSplitOptions.RemoveEmptyEntries);

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 []:

string input = @"[abc].[some other string].[can.also.contain.periods].[our [the] match]";
string[] seps2 = { "].["};
string[] splitted = input.Split(seps2, StringSplitOptions.RemoveEmptyEntries);

you our [the] match] in the last element (index 3) and you'd have to remove the extra ]

信仰 2024-09-13 16:15:14

您有多种选择:

  • RegexOptions.RightToLeft - 是的,.NET 正则表达式可以做到这一点!使用它!
  • 将整个内容与贪婪前缀匹配,使用括号捕获您感兴趣的后缀

参考文献

You have several options:

  • RegexOptions.RightToLeft - yes, .NET regex can do this! Use it!
  • Match the whole thing with greedy prefix, use brackets to capture the suffix that you're interested in
    • So generally, pattern becomes .*(pattern)
    • In this case, .*\[([^\]]*)\], then extract what \1 captures (see this on rubular.com)

References

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文