Click here to Skip to main content
15,885,954 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi
I need to take the numbers from the following string between the following characters :
string s = "Number of: Fails (2); Aborts (0); Warnings (1)";
need to take only:
2 , 0 , 1

Thanks.
Posted
Comments
PIEBALDconsult 24-Aug-15 9:46am    
Regular Expressions.
Member 10304617 24-Aug-15 9:51am    
what is the expression?
Thanks7872 24-Aug-15 10:23am    
Do you have access to Google? Try something yourself first.
Member 10304617 24-Aug-15 13:20pm    
I have access to google, tried to do something but I looked for something clever.
you don't need to answer if you don't want to.
Thanks7872 25-Aug-15 1:21am    
Show the efforts you have made.

I would use an expression for each of the numbers. You could do all in one using named groups, but three is easier to manage.

I'll use positive lookaheads and behinds for the capture:

C#
private Regex FailsRegex = new Regex(@"(?<=Number of: Fails \()\d*(?=\))");
private Regex AbortsRegex = new Regex(@"(?<=Aborts \()\d*(?=\))");
private Regex WarnsRegex = new Regex(@"(?<=Warnings \()\d*(?=\))");


let me break these down for you:

Three parts:
Non Captures LookBehind: (?<=Number of: Fails \()
Captured digits \d*
Non Captured LookAhead: (?=\))

The LookBehind "(?<=[regex]) checks the string before the captured part. In this case I use the text AND the bracket, which is escaped "\("

The capture \d* looks for zero to infinite digits [0-9]

The LookBehind "(?=[regex]) looks at the string after the captured part. in this case just the close bracket "/)"

Hope that helps ^_^
Andy

UPDATE:

Added the @ at the beginning of the regex strings.

The /d char is a valid escape sequence for a regex but not for a string. Each Regex escape would need to be escaped. The @ symbol is shorthand for this:

C#
@"(?<=Number of: Fails \()\d*(?=\))" = "(?<=Number of: Fails \\()\\d*(?=\\))"



UPDATE:
Some ideas that do not involve Regex:
C#
string[] items = s.Split(';'); // split the string into three using the ';' as a new item marker.
int fails = int.Parse(items[0]);  //gets the numbers only from a string.  Usually used to turn string "20" into int 20
int aborts = int.Parse(items[1]);
int warns = int.Parse(items[2]);

//or maybe using the positions?
string fails = s.Substring(s.FirstIndexOf("("),s.FirstIndexOf(")") - s.FirstIndexOf("(");
//that seems a bit needless in this case though.

They're your best options ^_^
 
Share this answer
 
v4
Comments
Member 10304617 24-Aug-15 10:10am    
got this error (about \d ):
Error 2 Unrecognized escape sequence
Andy Lanng 24-Aug-15 10:12am    
in c#? that should be fine.
Use the literal instead: replace \d* with [0-9]*
Andy Lanng 24-Aug-15 10:13am    
WAIT - I made an error
you need to have the @ in front of the Regex string so it will be literal.
I have updated my solution
Member 10304617 24-Aug-15 10:20am    
thanks
is there other way to get this numbers? without using regular expressions?
Andy Lanng 24-Aug-15 10:29am    
Plenty.
I'll write up a couple. Regex is generally best, but if you're new to it then it can be daunting.
Please see:
http://en.wikipedia.org/wiki/Regular_Expression[^],
https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex%28v=vs.110%29.aspx[^].

The solution will be very simple, but only if all the (*) sub-strings are in place and * can be parsed as number. For example, for integer, use int.Parse or int.TryParse:
https://msdn.microsoft.com/en-us/library/system.int32.parse%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx[^].

Credit to the comment to the question by PIEBALDconsult.

—SA
 
Share this answer
 
v2
Comments
Member 10304617 24-Aug-15 10:31am    
all numbers in place, and can be parsed as number.
but how can I get all numbers? what is the regular expression that I can insert the string and get this 3 numbers?
Sergey Alexandrovich Kryukov 24-Aug-15 10:43am    
I answered. You could use regular expression groups, with (). It could be "(\([0-9]+\))" (the rest of regular expression omitted). Your () are escaped, group () are added. Then you match regular expression, take groups out of it, and, for each group (check up that you have 3), parse the group's text.
—SA
Member 10304617 24-Aug-15 10:45am    
I don't know how to write the regular expression, you can't write it?
Sergey Alexandrovich Kryukov 24-Aug-15 12:05pm    
Do yourself a big favor, learn it by starting with your simple problem. This is not as hard as you can think. Note that I never use them for my own work, but easily learned it just to help other people when have some more tricky problems. Yours is not tricky at all, besides, I already gave you the key part of the expression. Add two more groups and some of .*? in between.
—SA
Member 10304617 24-Aug-15 11:50am    
thanks
 
Share this answer
 
Comments
Member 10304617 24-Aug-15 11:49am    
very helps!
thanks.
XML
string s = "Number of: Fails (2); Aborts (0); Warnings (1)";
        List<int> list=new List<int>();
        for (int i = 0; i < s.Length; i++)
        {
            if (Convert.ToInt32( s[i])>=48 && Convert.ToInt32( s[i])<=57)
            {
                string temp = s[i].ToString();
                list.Add(Convert.ToInt32( temp));
            }
        }
 
Share this answer
 
Comments
Member 10304617 24-Aug-15 11:50am    
thanks
Mehmet Uluağaç 25-Aug-15 10:08am    
It's not important
Philippe Mori 24-Aug-15 14:12pm    
Unmaintanable code. What is 48? There are functions in .NET to check if a character is a digit. Also the code would fails for number above 9.
Mehmet Uluağaç 25-Aug-15 10:08am    
no friend code is successful. Compare characters according to the ASC code. the ASC code table correspond to the numbers from 48 to 57 0-9. have a nice day
Philippe Mori 25-Aug-15 10:41am    
If the number of fails is 10, number of abort 2 and number of warning 3, then the above code would returns : 1, 0, 2 and 3 instead of 10, 2 and 3.

Hard-coded constants are evil. It can fails if the code is ported to a system using another char set. Also, you cannot assume that everyone knows ASC code range for digits.
C#
string xx = "this is first [1] and this is second: [2] this is third [3];
ArrayList r1 = new ArrayList();
char[] rgc = xx.ToCharArray();
foreach (char c in rgc) {

if (char.IsDigit(c)) { r1.Add(c);  }
 
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