Click here to Skip to main content
15,886,665 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello, I am blasting my head off with one reg exp and can't get it working on C#

the text it this

HTML
<span class="css-truncate-target">0.1.1.0</span>


I want to get the Version: 0.1.1.0

And the C# is:
C#
Match match = Regex.Match(releasesString, @"<span class=""css - truncate - target"">(?:[0-9].[0-9].[0-9].[0-9])<\/span>",
            RegexOptions.IgnoreCase);

            if (match.Success)
            {
                Console.WriteLine(match.Groups[1].Value);
            }


And the Success is always false
Posted
Comments
PIEBALDconsult 7-Dec-15 20:57pm    
You must be ready for a shot of Expresso
http://www.ultrapico.com/Expresso.htm
PIEBALDconsult 7-Dec-15 21:08pm    
You have SPACEs surrounding the HYPHENs in your Expression but not in the sample text -- so it can't succeed.
Oh, and the ?: in your group instructs the engine _not_ to capture, so you will still have trouble trying to access Groups[1] .
And why did you escape the SLASH?

Try this:
(?<=\<span.*\>)\d\.\d\.\d.\d(?=\</span\>)

The keys lie in:
(?<=exp) // zero-width positive lookahead assertion

and
(?=exp) // zero-width positive lookbehind assertion

Google and find out more about them.
 
Share this answer
 
Comments
BillWoodruff 8-Dec-15 1:27am    
+5
Peter Leow 8-Dec-15 1:46am    
Thank you, BillWoodruff.
Matt T Heffron 8-Dec-15 13:17pm    
+5
Peter Leow 8-Dec-15 20:44pm    
Thank you, Matt T Heffron.
You have too many problems here, even though the problem is extremely simple. First of all, '.' is a special Regular Expression notation. So, escape it: '\.'.
Take each '[0-9]' term in round brackets, it will help you to get set of matches and later parse each of the matches into integer. And finally, do you really want to have only one digit in each version component? I doubt it. Add multiplicity: '+' (which means 1 or more). Oh, and I almost forgot: add ^ at the beginning and $ at the end.
I hope after I described all your mistakes, you can easily solve the problem.

See also:
https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/e7sf90t3(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchcollection%28v=vs.110%29.aspx[^].

Note that I referenced the method Matches, not Match. This is what you really want, because you need to match each part of the version separately and parse all matches separately.

But I would solve the problem much simpler:
C#
string[] versionParts = versionString.Split('.');
//and then use int.TryParse for each element of the array versionParts


—SA
 
Share this answer
 
v3
Comments
BillWoodruff 8-Dec-15 1:27am    
My vote of #4: seems to me this answer has good suggestions on the RegEx, and, since I prefer to write code rather than use RegEx, I go for the idea of splitting the string, and then parsing the numbers as suggested here.

If you did split with the code shown here, element #0 would be:

<span class=\"css-truncate-target:">0

and the last element would be: 0</span>

char[] split = new[] {'.', '<', '>'}; // better split with this

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