如何捕捉M/S之间但没有最后唯一的单词

本文关键字:最后 唯一 单词 何捕捉 之间 | 更新日期: 2023-09-27 18:09:51

如何仅捕获以下内容的一部分。捕获应该限制在M/S和UniqueID之间。我的Regex (?<=M'/S's)(('w+)?'s)+(?=(U|$))捕获具有UniqueID的行,但不捕获没有UniqueID的行。谁能给我指路?

$0117578$ $0003123$ ADECBR5148K M/S R A MARKETING PRIVATE LIMITED
$0117942$ $0003487$ ADECBR6540D M/S RICHDAUGHTER KURIES AND INVESTMENTS PVT LIMITED
$0117856$ $0003401$ ADEBCR7789P M/S REGENT TRADING CO
$0117881$ $0003426$ ADEBCR1123M M/S RELIANT MARKETING AND SERVICES PVT LTD  U53599KL1989PTC000007
$0117896$ $0003441$ ADECBR7750D M/S GANDHIMARG CHITTIES PRIVATE LIMITED U64992KL2003PTC016359
$0117923$ $0003468$ ADEECR0117J M/S REVELATION AUTOMATIC PISTOLS PRIVATE LIMITED    U29300KL2011PTC020087
$0117927$ $0003472$ ADECBR9145C M/S RESSEA DOMESTIC APPLE PRIVATE LIMITED   U29208KL2004PTC010016
$0117746$ $0003291$ ADEGCR4072G M/S RANDOR MARITIME INC

如何捕捉M/S之间但没有最后唯一的单词

我认为你需要这样的代码(使用LinQ):

var list = new[]
{
    "$0117578$ $0003123$ ADECBR5148K M/S R A MARKETING PRIVATE LIMITED",
    "$0117942$ $0003487$ ADECBR6540D M/S RICHDAUGHTER KURIES AND INVESTMENTS PVT LIMITED",
    "$0117856$ $0003401$ ADEBCR7789P M/S REGENT TRADING CO",
    "$0117881$ $0003426$ ADEBCR1123M M/S RELIANT MARKETING AND SERVICES PVT LTD  U53599KL1989PTC000007",
    "$0117896$ $0003441$ ADECBR7750D M/S GANDHIMARG CHITTIES PRIVATE LIMITED U64992KL2003PTC016359",
    "$0117923$ $0003468$ ADEECR0117J M/S REVELATION AUTOMATIC PISTOLS PRIVATE LIMITED U29300KL2011PTC020087",
    "$0117927$ $0003472$ ADECBR9145C M/S RESSEA DOMESTIC APPLE PRIVATE LIMITED U29208KL2004PTC010016",
    "$0117746$ $0003291$ ADEGCR4072G M/S RANDOR MARITIME INC"
};
// Here I use named groups to capture what I want as `A`
// Format of UniqueId here is : `U[A-Z'd]{20}`
var regex = new Regex(@"(M'/S(?<A>.+)U[A-Z'd]{20})|(M'/S(?<A>.+)$)");
var matches = (from str in list                   // loop over `list` as `str`
    select regex.Match(str)                       // create new list of values from `str`
    into match                                    // into a new variable as `match`
    where match.Groups.Count > 1                  // filter new list by `match`
    select match.Groups["A"].ToString().TrimEnd() //create new list of values again
    ).ToList();