Kannan Nakkeeran
Kannan Nakkeeran

Reputation: 31

Regex to get values from a string using C#

I have posted this earlier but did not give clear information on what i was trying to achieve.

I am trying get values from a string using Regex in c#. I am not able to understand why some values i could get and some i can not using a similar approach.

Please find the code snippet below. Kindly let me know what i am missing. Thanks in advance.

string text = "0*MAO-001*20160409*20160408*Encounter Data Duplicates Report       *     *ENC000200800400120160407*PRO*PROD*";

//toget the value 20160409 from the above text
//this code works fine
Regex pattern = new Regex(@"([0][*]MAO[-][0][0][1].*?[*](?<Value>\d+)[*])");
Match match = pattern.Match(text);
string Value = match.Groups["Value"].Value.ToString();



//to get the value ENC000200800400120160407 from the above text
// this does not work and gives me nothing
Regex pattern2 = new Regex(@"([0][*]MAO[-][0][0][1].*?[*].*?[*].*?[*].*?[*].*?[*](?<Value2>\d+)[*])");
Match match2 = pattern.Match(text);
string Value2 = match.Groups["Value2"].Value.ToString();

Upvotes: 1

Views: 154

Answers (4)

Zee
Zee

Reputation: 840

It looks your file is '*' delimitered.

You can use one single regex to catch all the values

Try use

((?<values>[^\*]+)\*)

as your pattern.

All these values will be catched in values array.

----Update add c# code-----

string text = "0*MAO-001*20160409*20160408*Encounter Data Duplicates Report       *     *ENC000200800400120160407*PRO*PROD*";
Regex pattern = new Regex(@"(?<values>[^\*]+)\*");
var matches = pattern.Matches(text);
string Value = matches[3].Groups["values"].Captures[0];
string Value2 = matches[6].Groups["values"].Captures[0];

Upvotes: 1

rock321987
rock321987

Reputation: 11042

You need to use this for 2nd regex

([0][*]MAO[-][0][0][1].*?[*].*?[*].*?[*].*?[*].*?[*](?<Value2>\w+)[*])

\w is any character from set [A-Za-z0-9_]. You were using only \d which searches for digits [0-9] which was not the case

C# Code

Upvotes: 0

user557597
user557597

Reputation:

You almost got it, but the field you're looking for contains letters and digits.

This is your second regex kind of fixed.

([0][*]MAO[-][0][0][1].*?[*](?:.*?[*]){4}(?<Value2>.*?)[*])

 (                             # (1 start)
      [0] [*] MAO [-] [0] [0] [1] .*? [*] 

      (?: .*? [*] ){4}

      (?<Value2> .*? )              # (2)
      [*] 
 )                             # (1 end)

To make it a little less busy, this might be better

(0\*MAO-001.*?\*(?:[^*]*\*){4}(?<Value2>[^*]*)\*)

Upvotes: 0

KDecker
KDecker

Reputation: 7148

In your second try at using the regex, you are matching with pattern and not pattern2.

Match match2 = pattern.Match(text);
string Value2 = match.Groups["Value2"].Value.ToString();

You are also using the Groups from match and not match2.

This is why it is important to name your variables something meaningful to what they represent. Yes it may be a "pattern" but what does that pattern represent. When you use variables that are vaguely named it creates issues like these.

Upvotes: 0

Related Questions