1

There's a way to transform a data value into another defined in a regex pattern?

I mean, I want to define a pattern like a=1|b=2|c=3.

So when if I pass the a value to Regex it returns me 1. If b returns 2 ... etc.

It's that possible?

4
  • Could you provide a sample source value? Commented Nov 5, 2013 at 17:48
  • Why would you want to use a regex for this? It is not what they are made for. Commented Nov 5, 2013 at 17:49
  • Why do you want to use regex for a simple replace scenario? Commented Nov 5, 2013 at 17:59
  • 1
    it's not even a replacement -- just KVPs. Commented Nov 5, 2013 at 18:01

4 Answers 4

3
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (Match m in Regex.Matches("a=1|b=2|c=3", @"\w?=\d?"))
{
    string[] val = m.Value.Split('=');
    dic.Add(val[0], Int32.Parse(val[1]));
}

Or

string val = "a";
Int32.Parse(Regex.Match("a=1|b=2|c=3", val + @"=(\d)").Groups[1].Value);
Sign up to request clarification or add additional context in comments.

3 Comments

@Maximus-Decimus Because the sample input you've provided is delimited key value pairs, this is a very strong solution. I don't think there's a need for you to do your work with Regexes given how simple and well formatted your input is.
I think the OP said a=1|b=2|c=3 is the pattern example, not the string to be parsed. So, like if he passes in "a", it returns 1.
My mistake, I didn't see you are just parsing for dictionary values/and/or hardcoding a interpolation string.
2

You can do this in C# like this:

var input = "a,  b,  c";
Dictionary<string, string>  lookup = new Dictionary<string, string>()
{
    {"a", "1"},
    {"b", "2"},
    {"c", "3"}
};
string result = Regex.Replace(input, "[abc]", m => lookup[m.Value] , RegexOptions.None);
Console.WriteLine(result); // outputs 1,  2,  3

I have used the regular expression [abc] which matches either a, b or c then depending on the match, the delegate used inside Replace() looks the match inside a dictionary to decide what to replace it with.

3 Comments

This is good. It only needs the match. Is the dictionary like an hash array?
@sln Then you would use the alternation. It seems that the OP didn't even want this which surprises me, but it doesn't matter now because the problem was solved. Thank you.
If you had a hash array, I think a single match with or without alternations would do it. return hash[match[0].val] or like that.
0

You could use Regex.Replace with a delegate to evaluate the matches.

See: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator(v=vs.110).aspx and: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.replace(v=vs.110).aspx

1 Comment

+0: almost nice answer - adding sample code that applies to OP case would make it much better.
0

The answer is NO. Regex only return match sucess/fail and that which matches the pattern.

You could however determine a group number match, and that may enable you to translate
that into a value or whatever you want.
But be sure the power of regex is really needed for the job, and not just a simple string compare.
Otherwise you could build a custom trie.

Pseudo code:

 pattern = @"
      ( Enie )         # (1)
   |  ( Menie )        # (2)
   |  ( Minie )        # (3)
   |  ( Moe )          # (4)
 ";

 int GetValue( string& str )
 {
   smatch match;
   if ( regex_find ( pattern, str, match, flags.expanded ) )
   {
       if ( match[1].matched )
            return val1;
       if ( match[2].matched )
            return val2;
       if ( match[3].matched )
            return val3;
       if ( match[4].matched )
            return val4;
   }
 }

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.