Click here to Skip to main content
14,774,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello again I have successfully parsed the string from my HTML output but am having problems if the string does not exist

For example I have a string that states the number of kids in a class that are sick.
"3 out of 41" but that number always changes so I need a way to identify the "out of" and also the two integers 3 and 41 that are just part of the string really.

Can I somehow use a wild card to represent the numbers if I do not know there exact value?


I hope someone knows what I am trying to say and knows the answer

thank you in advance!
Posted

So, you have:
number out of number


A number is a sequence of digits, with at least one digit:
Regex digit: \d
At least one: +

Now you my want to extract these two numbers: (\d+)

Putting all together (C#):
string pattern = @"(\d+) out of (\d+)";


Getting all matches in the whole text (C#):
string fullText = ...;
string pattern = @"(\d+) out of (\d+)";
int all = 0;
int sick = 0;
int count = 0;
foreach(var match in Regex.Matches(fullText, pattern).Cast<Match>())
{
    sick += int.Parse(match.Group[1].Value);
    all += int.Parse(match.Group[2].Value);
    count++;
}
Console.WriteLine("From {0} classes, {1}% are sick",
                   count, all > 0 ? sick / all : 0); 


Cheers
Andi
   
Comments
Dale 2012 7-Sep-12 7:39am
   
Works Beautifully YOU ARE awesome thank you so much for this !!!!
Dale 2012 7-Sep-12 7:39am
   
thank you thank you!!
Andreas Gieriet 7-Sep-12 16:28pm
   
You are welcome.
Andi
Dale 2012 10-Sep-12 0:07am
   
Can you please help me with a slight problem match?....

I am looking to match an unknown string after my match word "Today Date: ? "
the string is ended by a "," so all together it looks like this

"Today Date: Monday-23," (without the quotes)

I am trying to match the string value Monday-23 but the value or words in that part of the string are not always the same. Is it possible to match the start of the string and then to somehow determine the end of the string to only show whats in the middle of that string.....

start of string is "Today Date:"
middle of string is = "Monday-23"
end of string is = ","

thank you in advance for any insight you may have to this

cheers!!
Andreas Gieriet 10-Sep-12 3:25am
   
I strongly suggest that you take the time and learn some basics first yourself.
You are now asking about 130 questions over the last 23 months - 1-2 question a week, at least. You will never get to a decent level of proficiency if you do not develop the solutions yourself. You seem not to understand what Regex does for you and how to define them. I'm not talking about the advanced stuff, I'm talking about Regex 101. Take one of the tutorials or take a good book on that. E.g.
Tutorials:
http://www.dotnetperls.com/regex-match
http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial
References:
http://msdn.microsoft.com/en-us/library/ms228595(v=vs.80).aspx
http://www.radsoftware.com.au/articles/regexsyntaxadvanced.aspx
http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet

Cheers
Andi
hii

try Expresso tool

http://www.ultrapico.com/Expresso.htm[^]
   
Comments
Dale 2012 7-Sep-12 7:28am
   
ok so far I got it to display "out of 42" which is so close to what i need but I cannot seem to get the first value to show

eg. "6 out of 42"

this is the string i have so far

Dim Detection As New Regex("out of .{1,3}", RegexOptions.IgnoreCase)

any ideas how i might fix this?

thank you for the useful tool
Ganesh Nikam 7-Sep-12 7:36am
   
read this will help you
http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial?msg=4344772#xx4344772xx
Dale 2012 7-Sep-12 7:44am
   
thank you for this friend !! the program seems a bit confusing but with this I should do fine thank you all for your help I appreciate it very much
Ganesh Nikam 7-Sep-12 7:48am
   
if you think then mark as accepted to this ans
Dale 2012 7-Sep-12 9:07am
   
I have got to all most the end of whats needed but seem to be having some problems understanding the circumstance of what to do when I need to find a string by the first few words and then to display whats to the right of it which is then ended by a , mark?
Dale 2012 7-Sep-12 9:08am
   
Also i do not wish to display the first few words I used to find the word that comes after it

any ideas?
Andreas Gieriet 7-Sep-12 16:27pm
   
What exactly is your additional problem at hand? Your statements are a bit confusing.
Andi
Ganesh Nikam 7-Sep-12 23:43pm
   
bit confusing just explain with example

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