开发者

regex split and extract multiple parts from a string

I am trying to extract some parts of the "Video:" line from below text.

Seems stream 0 codec frame rate differs from container frame rate: 30000.00 (300
00/1) -> 14.93 (1000/67)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\a.3gp':
  Metadata:
    major_brand     : 3gp5
    minor_version   : 0
    compatible_brands: 3gp5isom
  Duration: 00:00:45.82, start开发者_开发百科: 0.000000, bitrate: 357 kb/s
    Stream #0.0(und): Video: mpeg4, yuv420p, 352x276 [PAR 1:1 DAR 88:69], 344 kb
/s, 14.93 fps, 14.93 tbr, 90k tbn, 30k tbc
    Stream #0.1(und): Audio: aac, 16000 Hz, mono, s16, 11 kb/s
    Stream #0.2(und): Data: mp4s / 0x7334706D, 0 kb/s
    Stream #0.3(und): Data: mp4s / 0x7334706D, 0 kb/s*

This is an output from ffmpeg command line where i can get Video: part with

private string ExtractVideoFormat(string rawInfo)
{
    string v = string.Empty;
    Regex re = new Regex("[V|v]ideo:.*", RegexOptions.Compiled);
    Match m = re.Match(rawInfo);
    if (m.Success)
    {
        v = m.Value;
    }
    return v;
}

and result is

mpeg4, yuv420p, 352x276 [PAR 1:1 DAR 88:69], 344 kb

What i am trying to do is to somehow split that line and get

mpeg4
yuv420p
352x276 [PAR 1:1 DAR 88:69]
344 kb

assigned to different string objects instead of single


String[] words = result.Split(", " , StringSplitOptions.None)

Will give you the following words in the array (I'm putting them on new lines just to make it clearer what is returned)

mpeg4

yuv420p

352x276

[PAR 1:1 DAR 88:69]

344 kb


You can split the string using String.Split().

string[] parts = String.Split(new [] { ", " }, text);

But when I use your expression it matches the following.

Video: mpeg4, yuv420p, 352x276 [PAR 1:1 DAR 88:69], 344 kb /s, 14.93 fps, 14.93 tbr, 90k tbn, 30k tbc Stream #0.1(und): Audio: aac, 16000 Hz, mono, s16, 11 kb/s Stream #0.2(und): Data: mp4s / 0x7334706D, 0 kb/s Stream #0.3(und): Data: mp4s / 0x7334706D, 0 kb/s*

This may be due to line breaks in your string.

You could use the following expression

[Vv]ideo:(,? *(?<item>[^,])+)*

and capture all interesting parts in the named group item without the need to perform additional splitting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜