1

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();
3
  • try with this for 2nd regex :- ([0][*]MAO[-][0][0][1].*?[*].*?[*].*?[*].*?[*].*?[*](?<Value2>\w+)[*]) Commented Apr 20, 2016 at 17:58
  • wow .. thanks .. it worked .. do u want to post it as answer.. what is "w+" for ? Commented Apr 20, 2016 at 18:02
  • You should split on * instead of using specific field validation that could miss something. Commented Apr 20, 2016 at 18:22

4 Answers 4

1

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];
Sign up to request clarification or add additional context in comments.

1 Comment

I think you had it right the first time. To use the capture collection though, it should be quantified (?:(?<values>[^\*]*)\*)+
0

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

Comments

0

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.

1 Comment

You are right .. that was a blunder.. thanks for correcting.. but the pattern2 is not getting me the result as well
0

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>[^*]*)\*)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.