2

我有字符串,["02-03-2013#3rd Party Fuel", "-1#Archived", "2#06-23-2013#Newswire"],我想将其分解为几个部分。这些字符串以日期和索引键为前缀并包含名称。

我设计了一个RegEx正确匹配每个键的。但是,如果我想一举匹配索引键、日期键和名称。只找到第一个键。递归组似乎没有像我预期的那样工作。

private const string INDEX_KEY_REGEX = @"(?<index>-?\d+)";
private const string DATE_KEY_REGEX = @"(?<date>(?:0?[1-9]|1[012])-(?:0?[1-9]|[12]\d|3[01])-\d{4})";
private const string KEY_SEARCH_REGEX = @"(?<R>(?:^|(?<=#))({0})#(?(R)))(?<name>.*)";

private string Name = "2#06-23-2013#Newswire"
... = Regex.Replace(
    Name,
    String.Format(KEY_SEARCH_REGEX, INDEX_KEY_REGEX + "|" + DATE_KEY_REGEX),
    "${index}, ${date}, ${name}"
);

// These are the current results for all strings when set into the Name variable.

// Correct Result: ", 02-03-2013, 3rd Party Fuel"
// Correct Result: "-1, , Archived"
// Invalid Result: "2, , 06-23-2013#Newswire"
// Should be: "2, 06-23-2013, Newswire"

敏锐的眼睛能看到我错过的东西吗?


我需要的最终解决方案

事实证明我不需要递归组。我只需要 0 到多个序列。这里是完整的RegEx

(?:(?:^|(?<=#))(?:(?<index>-?\d+)|(?<date>(?:0?[1-9]|1[012])-(?:0?[1-9]|[12]\d|3[01])-(\d{2}|\d{4})))#)*(?<name>.*)

并且,分段RegEx

private const string INDEX_REGEX = @"(?<index>-?\d+)";
private const string DATE_REGEX = @"(?<date>(?:0?[1-9]|1[012])-(?:0?[1-9]|[12]\d|3[01])-(\d{2}|\d{4}))";
private const string KEY_WRAPPER_REGEX = @"(?:^|(?<=#))(?:{0})#";
private const string KEY_SEARCH_REGEX = @"(?:{0})*(?<name>.*)";
4

1 回答 1

1

好吧,各个正则表达式分解为:

索引:捕获单个正数或负数。(-、0 或 1 个代表,后跟一位或多位数字)

date:指定日期字符串,用-分隔。不考虑任何其他日期格式。请注意,不处理前导 '#' 和尾随 '#',它专门捕获日期,并且仅捕获日期

R:行首或#,然后是格式替换,使其成为一个大正则表达式......然后是另一个#,指定。然后一个没有假的条件......并且真也不做任何事情。

名称:捕获剩下的任何东西。

最终结果,编译成一个正则表达式......两个捕获:R 和名称。R:(4 部分) R-1:匹配行首或 # R-2:获取任何一个(但从不同时获取)日期或索引 R-3:匹配 # R-4:空条件表达式名称:匹配剩下的任何内容.

问题似乎是您不匹配索引和日期

最终编辑,工作正则表达式

忍受我,这件事很讨厌。您必须考虑所有 4 种可能性,否则它不会匹配所有可能的情况。我想不出任何方法来概括它。

(?:(?<index>-?\d+(?!\d-))#(?<date>(?:0?[1-9]|1[012])-(?:0?[1-9]|[12]\d|:3[01])-\d{4})|(?<date>(?:0?[1-9]|1[012])-(?:0?[1-9]|[12]\d|:3[01])-\d{4})#(?<index>-?\d+)|(?!-?\d+#)(?<date>(?:0?[1-9]|1[012])-(?:0?[1-9]|[12]\d|:3[01])-\d{4})|(?<index>-?\d+)(?!#(?:0?[1-9]|1[012])-(?:0?[1-9]|[12]\d|:3[01])-\d{4}))#(?<name>.*)

丑陋,我知道。它有 4 个初始条件。

1a) capture <index>#<date>  OR
1b) capture <date>#<index>  OR
1c) capture <index> only, as long as its not followed by a date  OR
1d) capture <date> only, as long as its not preceded by an index
...
2) match but ignore #
3) capture <name>

适用于所有 4 种情况。

最终:最终编辑

有一种方法可以使用 3 个正则表达式而不是 1 个来执行此操作,这最终可能会更干净。

//note: index MIGHT be preceeded by, and is ALWAYS followed by, a #
indexRegex = @"((?=#)?(?<!\d|-)-?\d+(?=#))";
//same with date
dateRegex = @"((?=#)?(?:0?[1-9]|1[012])-(?:0?[1-9]|[12]\d|3[01])-\d{4}(?=#))";
//then name
nameRegex = @"(?:.*#){1,2}(.*)";

分别针对替换运行它们以获取各个变量,然后重建字符串。

于 2013-04-10T16:44:04.730 回答