简体   繁体   English

匹配字符串,由-使用正则表达式分隔

[英]Matching string separated by - using regex

Regex is not my favorite thing, but it certainly has it's uses. 正则表达式不是我最喜欢的东西,但是它肯定有它的用途。 Right now I'm trying to match a string consisting of this. 现在,我正在尝试匹配包含此内容的字符串。

[video-{service}-{id}] 

An example of such a string: 这样的字符串的示例:

[video-123abC-zxv9.89] [视频123ABC-zxv9.89]

In the example above I would like to get the "service" 123abC and the "id" zxv9.89. 在上面的示例中,我想获取“服务” 123abC和“ id” zxv9.89。

So far this is what I've got. 到目前为止,这就是我所拥有的。 Probably overcompliacated.. 可能过分复杂了。

var regexPattern = @"\[video-(?<id1>[^]]+)(-(?<id2>[^]]+))?\]";
var ids = Regex.Matches(text, regexPattern, RegexOptions.IgnoreCase)
    .Cast<Match>()
    .Select(m => new VideoReplaceItem()
    {
        Tag = m.Value,
        Id = string.IsNullOrWhiteSpace(m.Groups["id1"].Value) == false ? m.Groups["id1"].Value : "",
        Service = string.IsNullOrWhiteSpace(m.Groups["id2"].Value) == false ? m.Groups["id2"].Value : "",
    }).ToList();

This does not work and puts all the charachters after '[video-' into into Id variable. 这不起作用,并将“ [视频-”之后的所有字符都放入Id变量中。

Any suggestions? 有什么建议么?

The third part seems to be optional. 第三部分似乎是可选的。 The [^]]+ is actually matching the - symbol, and to fix the expression, you either need to make the first [^]]+ lazy ( [^]]+? ) or add a hyphen to the negated character class. [^]]+实际上与-符号匹配,并且要修复表达式,您需要使第一个[^]]+惰性( [^]]+? )或在否定的字符类中添加连字符。

Use 采用

\[video-(?<id1>[^]-]+)(-(?<id2>[^]-]+))?]

See the regex demo 正则表达式演示

Or with the lazy character class: 或使用惰性字符类:

\[video-(?<id1>[^]]+?)(-(?<id2>[^]]+))?]
                    ^

See another demo . 参见另一个演示

Since you are using named groups, you may compile the regex object with RegexOptions.ExplicitCapture option to make the regex engine treat all numbered capturing groups as non-capturing ones (so as not to add ?: after the ( that defines the optional (-(?<id2>[^]-]+))? group). 既然你是使用命名组,您可以编写regex对象与RegexOptions.ExplicitCapture选项使正则表达式引擎对待所有编号的捕获组作为非捕获者(以免增加?:(即定义了可选的(-(?<id2>[^]-]+))?组)。

Try this: 尝试这个:

\[video-(?<service>[^]]+?)(-(?<id>[^]]+))?\]

The "?" “?” in the service group makes the expression before it "lazy" (meaning it matches the fewest possible characters to satisfy the overall expression). 服务组中的表达式使表达式在其之前成为“惰性”(这意味着它与尽可能少的字符匹配才能满足整个表达式)。

I would recommend Regexstorm.net for .NET regex testing: http://regexstorm.net/tester 我会推荐Regexstorm.net进行.NET regex测试: http ://regexstorm.net/tester

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM