简体   繁体   中英

C# regular expression

I have string like this:

{F971h}[0]<0>some result code: 1

and I want to split it into:

  • F971
  • 0
  • 0
  • some result code: 1

I know I can first split "{|}|[|]|<|> " it into:

  • {F971h}
  • [0]
  • <0>
  • some result code: 1

and next: {F971h} -> F971; [0] -> 0; etc.

But how can I do it with one regular expression? I try somethink like this:

Regex rgx = new Regex(@"(?<timestamp>[0-9A-F]+)" + @"(?<subsystem>\d+)" + @"(?<level>\d+)" + @"(?<messagep>[0-9A-Za-z]+)");
var result = rgx.Matches(input);

You can try just Split without any regular expressions:

string source = "{F971h}[0]<0>some result code: 1";

string[] items = source.Split(new char[] { '{', '}', '[', ']', '<', '>' },
  StringSplitOptions.RemoveEmptyEntries);

Test:

// F971h
// 0
// 0
// some result code: 1
Console.Write(String.Join(Environment.NewLine, items));    

You can get it like that:

string line = @"{F971h}[0]<0>some result code: 1";

var matchCollection = Regex.Matches(line, @"\{(?<timestamp>.*?)\}\[(?<subsystem>.*?)\]<(?<level>.*?)>(?<messagep>.*)");
if (matchCollection.Count > 0)
{
    string timestamp = matchCollection[0].Groups["timestamp"].Value;
    string subsystem = matchCollection[0].Groups["subsystem"].Value;
    string level = matchCollection[0].Groups["level"].Value;
    string messagep = matchCollection[0].Groups["messagep"].Value;
    Console.Out.WriteLine("First part is {0}, second: {1}, thrid: {2}, last: {3}", timestamp, subsystem, level, messagep);
}
else
{
    Console.Out.WriteLine("No match found.");
}

You can watch it live here on regex storm . You'll have to learn about:

There are two issues with your regex:

  • You do not allow lowercase ASCII letters in the first capture group (add az or a RegexOptions.IgnoreCase flag)
  • The delimiting characters are missing in the pattern ( < , > , [ , ] , etc.)

Use

{(?<timestamp>[0-9a-zA-F]+)}\[(?<subsystem>\d+)]<(?<level>\d+)>(?<messagep>.+)
^                 ^^^      ^^^                 ^^             ^

See the regex demo

Since the messagep group should match just the rest of the line, I suggest just using .+ at the end. Else, you'd need to replace your [0-9A-Za-z]+ that does not allow whitespace with something like [\\w\\s]+ (match all word chars and whitespaces, 1 or more times).

在此处输入图片说明

C# code :

var s = @"{F971h}[0]<0>some result code: 1";
var pat = @"{(?<timestamp>[0-9a-zA-F]+)}\[(?<subsystem>\d+)]<(?<level>\d+)>(?<messagep>.+)";
var m = Regex.Match(s, pat);
if (m.Success)
{
    Console.Out.WriteLine(m.Groups["timestamp"].Value);
    Console.Out.WriteLine(m.Groups["subsystem"].Value);
    Console.Out.WriteLine(m.Groups["level"].Value);
    Console.Out.WriteLine(m.Groups["messagep"].Value);
}

Or for a multiline string containing multiple matches:

var s = "{F971h}[0]<0>some result code: 1\r\n{FA71h}[0]<0>some result code: 3\r\n{FB72h}[0]<0>some result code: 5";
var pat = @"{(?<timestamp>[0-9a-zA-F]+)}\[(?<subsystem>\d+)]<(?<level>\d+)>(?<messagep>[^\r\n]+)";
var res = System.Text.RegularExpressions.Regex.Matches(s, pat)
     .Cast<System.Text.RegularExpressions.Match>()
     .Select(x => new[] { 
            x.Groups["timestamp"].Value, 
            x.Groups["subsystem"].Value,
            x.Groups["level"].Value,
            x.Groups["messagep"].Value})
      .ToList();

在此处输入图片说明

Thank you all! Code below works for me. I missed that it can be multiple string:

{F971h}[0]<0>some result code: 1\r\n{FA71h}[0]<0>some result code: 3\r\n{FB72h}[0]<0>some result code: 5

code:

        var pat = @"{(?<timestamp>[0-9a-zA-F]+)}\[(?<subsystem>\d+)]<(?<level>\d+)>(?<message>.+)";
        var collection = Regex.Matches(input, pat);

        foreach (Match m in collection)
        {
            var timestamp = m.Groups["timestamp"];
            var subsystem = m.Groups["subsystem"];
            var level = m.Groups["level"];
            var message = m.Groups["message"];
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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