YAKOVM
YAKOVM

Reputation: 10153

Extract argumens values from the format string

I have a string which is actually a format for example "m_{0}A1_{1}Tmp" given such format string and data I need to extract the values of the arguments i.e. {0} and {1} Following the example above if the data is m_TomerA1_DbTmp , I should extract that the first argument is Tomer and the second is Db Will be glad to hear how can I do it? My intuition says that I need regular expression here...

Upvotes: 0

Views: 73

Answers (1)

zx81
zx81

Reputation: 41838

Use this regex and retrieve the Group 1 and Group 2 matches:

m_(.*?)A1_(.*?)Tmp

On the demo, look at the captured groups on the right.

In C#:

var myRegex = new Regex("m_(.*?)A1_(.*?)Tmp");
Match theMatch = myRegex.Match(yourString);
String token1 = theMatch.Groups[1].Value;
String token2 = theMatch.Groups[2].Value;

Explanation

  • m_ matches literal characters
  • (.*?) captures to Group 1 and lazily matches everything up to...
  • A1_ literal
  • (.*?) captures to Group 2 and lazily matches everything up to...
  • Tmp literal
  • the Group i is retrieved with theMatch.Groups[i].Value

Upvotes: 1

Related Questions