Monday, November 26, 2018

Why does it not print the last Regex group in C#





I have written this very straight forward regex code



using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)

{

string lines = "2019-05-07 11:50:28, INFO Ayush Singhania, Level 1, EID: 1001, UID: ayush.power, Message: Create a Job";
Regex pattern = new Regex(@"(?.*?\s)(?


The Output for the following is:




2019-05-07
11:50:28
Ayush Singhania
Level 1
1001
ayush.power
........... (The Last Line is Blank)



Everything is working fine excel for the last group - "Message"
It does not print anything. Can anyone suggest why is it so?
Also i think my regex pattern is correct. Check it here https://regex101.com/r/ysawNT/1


Answer



Apart from using the wrong message Groups["Message"] which should be MSG, the last part is empty because the last part has no boundary set and the non greedy .*? is also satisfied to match nothing.



What you could do is match the rest of the string using (?.*)



Regex demo | C# demo




enter image description here


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...