Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi everyone,

I have come against a scenario where i have to extract all values where it occurs in a large string. For example:

C#
string longString = "val{1}:val{5}/val{10}";


So i want to extract all values that occur where val{ };

The result should be:
1
5
10

I have been trying to do this using indexOf but with not much luck. What would be easy way to accomplish this?

I'd appreciate any advice! :)
Posted
Comments
Matt T Heffron 16-Sep-15 13:33pm    
What additional constraints are there on the string?
E.g., Will the values always be positive integers? Or could there be negative numbers or real values?
Could digit characters occur anywhere else in the string, outside of val{}? Should those be ignored?
Will you also need to do other processing of the string (so doing it all at once may be more efficient)?
...
Silver13 16-Sep-15 19:33pm    
The numbers are always going to be inside val{} for this scenario.
The target is to get those numbers inside. The only other constrain for this would to ignore zeros.

C#
string longString = "val{1}:val{5}/val{10}";
var Digits = Regex.Matches(longString, "[0-9]+");
List<string> LstDigit= new List<string>();
foreach (Match Digit in Digits)
{    
    LstDigit.Add(Digit.ToString());
}

Regards..
 
Share this answer
 
v2
Comments
Silver13 16-Sep-15 19:34pm    
Pretty straight forward. I always underestimate the power of Regex. This is a great solution!
Hello ,
Try this way
string longString = "val{124}:val{50}/val{100}";
		//first split the string into two parts from semicolon
		string[] sp=longString.Split(':');
		
		//then get the first part by using substring , here 1234
		
		Console.WriteLine("First Part is " + sp[0].ToString().Substring(4,sp[0].ToString().Length-5));
		
		
		//again split the first array into two parts 
		string second=sp[1].ToString() ;
		string[] secondarrary=second.Split('/'); 
		
		//Now get second and third part using substring 
		Console.WriteLine("Second Part is " + secondarrary[0].ToString().Substring(4,secondarrary[0].Length-5));
		Console.WriteLine("Third  Part is " + secondarrary[1].ToString().Substring(4,secondarrary[1].Length-5));


and OP is
First Part is 124
Second Part is 50
Third  Part is 100
 
Share this answer
 
add the reference
using System.Text.RegularExpressions;

Then add the following code
string[] numbers = Regex.Split("val{1}:val{5}/val{10}", @"\D+").Where(x => !string.IsNullOrEmpty(x)).ToArray();


And finally you will get a string array which will contain all the numbers
 
Share this answer
 
v2
So, based on your answer to my questions above:
C#
// create the Regex only once!
private static readonly Regex ValContents = new Regex("val{([^}]+)}", RegexOptions.Compiled);
// in the appropriate method:
string longString = "val{1}:val{5}/val{10}";
MatchCollection matches = ValContents.Matches(longString);
List<string> values = matches.Cast<Match>().Select(m => m.Groups[1].Value).ToList();

This will return a list of all of the content of each val{} as a string.
If you want it to ignore any val{} content that is not simply digits, then change the Regex string to: "val{(\d+)}"
 
Share this answer
 
C#
str = "val{1}:val{5}/val{10}";
      StringBuilder sb = new StringBuilder(str);
      sb = sb.Replace("val{", string.Empty)
      .Replace("val{", string.Empty)
      .Replace("}:", ",")
      .Replace("}/", ",")
      .Replace("}", string.Empty);
       Console.Write( sb.ToString());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900