Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to know the regular expression for the string ends with % symbol(Ex:"0.000%" this is my string ) could any one tell me the answer....
Posted

Try:
\d+\.\d{3}%$
It matches 1 or more digits, decimal point, 3 digits, percent at the end of the string.

Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
Comments
Herman<T>.Instance 6-Feb-13 8:16am    
Good tip, My 5. Now installing Expresso
VahanHakobyan 4-Apr-16 10:33am    
What about any amount of digits after point?
OriginalGriff 4-Apr-16 11:05am    
Replace the "{3}" with "+" - it says "one or more"
Parsing numbers might fool you if you deal with software that must run under variing cultures.
The decimal separator is not the same on all cultures.

Depending on where you got your string from (e.g. is it localized or culture invariant?) then you must follow differing approaches.

If it is culture invariant, take one of the other suggested soulutions.
If localized, let the system parse the number, i.e. take the non-space text before the % symbol and pass it to the TryParse method.
E.g.
C#
string s = "...";
var match = Regex.Match(s, @"^\s*(.*?)\s*%\s*$");
double percentage;
NumberStyles style = NumberStyles.Currency; // allow all but exponential form
CultureInfo culture = CultureInf.CurrentCulture;
if (match.Success && double.TryParse(match.Groups[1].Value, style, culture, out percentage))
{
   // do something with percentage...
   ...
}
else
{
   // handle error
   ...
}

Cheers
Andi
 
Share this answer
 
Try:
C#
.*%$


This will match with any string ending with %

Or

C#
[0-9]*.[0-9]*%$


This will match with any number with single decimal and ending with %

You can create your own or try this on http://regexhero.net/
 
Share this answer
 
v2
Comments
OriginalGriff 6-Feb-13 8:35am    
Um.
"[0-9]*.[0-9]*%$"
"This will match with any number with single decimal and ending with %"
Are you sure?
Have you tried "X%" - because it will match that as well...
T Pat 6-Feb-13 9:08am    
Ya that matched...
Can you please tell me the reason behind that...
OriginalGriff 6-Feb-13 9:33am    
Two reasons:
1) "*" is "any number of, including zero". So "[0-9]*" is "zero or more digits"
2) "." is "any character".
So "[0-9]*.[0-9]*%$" happily matches "nothing, any character, nothing, percent, end of string"
What you actually wanted was "[0-9]+\.[0-9]+%$" which requires "at least one digit, a literal decimal point, at least one digit, percent, end of string"

BTW: "[0-9]" can be replaced with "\d"

Pick up a copy of Expresso (the address is in my post) - it's free and it makes working with regexes a whole lot easier! I really wish I'd written it...:sigh:
Hi,

I am not sure this will help you, however thought of passing this link to you.

http://oreilly.com/windows/archive/csharp-regular-expressions.html[^]

The htmlcode for % = amb hash thirty seven (& # 3 7)
http://www.web-source.net/symbols.htm[^]


I will post once again if have an accurate answer.

Thanks,
B.S
 
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