当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Reject lines with time

问题描述:

I need a regex that could select from the following text:

 string test hello world!

bitmap player player.png

terrain(test)

bg(sky)

label(asdasd,sd, sd,ad,adsad, ds){sdds}

00:30 test(asda,asdad,adsd)asdad{asd}

02:30 test(asda,asdad,adsd)asdad

00:40 test(asda,asdad,adsd)asdad

returns the following groups:

{

"string test hello world!",

"bitmap player player.png",

"terrain(test)",

"bg(sky)",

"label(asdasd,sd, sd,ad,adsad, ds){sdds}"

}

i'd like to use ..:.. for avoiding the time.

thanks a lot.

i tried

(?<!\b..:..\s).*

but didnt work.

网友答案:

use this (with multiline flag):

^(?!\s*[0-9]{2}\:[0-9]{2})\s*(?<captured>.+)$
网友答案:

So .. You want any line not starting with a number? It's not very clear what your criteria are from your original question.

You could try:

^ *(?![0-9 ])(.+?) *$

Meaning, "start of line followed by spaces, followed by anthing that isn't a digit or a space, ended by spaces".

网友答案:

Try this, I used additionally RegexOptions.IgnorePatternWhitespace that allows readable regexes and comments in regexes as well.

String s = @"string test hello world!
    bitmap player player.png
terrain(test)
bg(sky)
label(asdasd,sd, sd,ad,adsad, ds){sdds}
00:30 test(asda,asdad,adsd)asdad{asd}
    02:30 test(asda,asdad,adsd)asdad
00:40 test(asda,asdad,adsd)asdad";

MatchCollection result = Regex.Matches
    (s, @"^                  # Match the start of the row (because of the Multiline option)
          ?!\s*\d{2}:\d{2})  # Row should not start with \d{2}:\d{2}
          \s*(.*)            # Match the row
          $"                 // Till the end of the row (because of the Multiline option)
          ,RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);

foreach (Match item in result) {
    Console.WriteLine(item.Groups[1]);
}
Console.ReadLine();
分享给朋友:
您可能感兴趣的文章:
随机阅读: