Regular Expression

Hi,
I need a regular expression. The regular expression should match all the types
 in the example given.
1.$ decimal money Ex: $.50, $1.50,$10.78
2.Decimals% Symbol Ex:9.15%, 1.15%,10.66%
3.single or double digits Ex:1, 22
4.only alphabets
5.alphabets and numbers

Thanks.
KavyaVSAsked:
Who is Participating?
 
Terry WoodsIT GuruCommented:
To accept that example, I've added a space character to the set of acceptable characters:
^(\\$(?=\\.?\\d)\\d*(\\.\\d\\d?)?|\\d*\\.\\d+%|\\d\\d?|[a-zA-Z\\d() ]+)$ 

Open in new window


If you'd like "other special characters" accepted, you'll need to define them.
0
 
käµfm³d 👽Commented:
For which language or editor?
0
 
Terry WoodsIT GuruCommented:
Something like this then maybe?

\$\d*\.\d\d|\d+\.\d+%|\d\d?|[a-zA-Z\d]+

I've assumed dollar and percent values always have a decimal point, and percent values always have at least one digit before the decimal.

kaufmed's right to ask which tool the pattern will apply to though. My pattern should work with several languages but not all.

Additionally, if used for validation, you will likely need a ^ and $ to be applied to the start and end respectively, to ensure no other characters occur before or after the matched value:

^(\$\d*\.\d\d|\d+\.\d+%|\d\d?|[a-zA-Z\d]+)$
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
käµfm³d 👽Commented:
I was thinking along these lines:

(\$([0-9]+(\.[0-9]+)?|(\.[0-9]+)))|(([0-9]+(\.[0-9]+)?|(\.[0-9]+))%)|([a-zA-Z]+)|([a-zA-Z0-9]+)|([0-9]{1,2})

Open in new window


It should match most editors and languages. I've taken the approach, however, that you can have items such as "$10" or "3%". It is not clear from your examples as to whether or not those are valid inputs.
0
 
KavyaVSAuthor Commented:
I am using C# and VisualStudio 2010.
items such as "$10" or "3%" are also valid.

Thanks
0
 
Terry WoodsIT GuruCommented:
Then an updated version of my pattern is:
\$(?=\.?\d)\d*(\.\d\d?)?|\d+(\.\d+)?%|\d\d?|[a-zA-Z\d]+
or, for validation:
^(\$(?=\.?\d)\d*(\.\d\d?)?|\d+(\.\d+)?%|\d\d?|[a-zA-Z\d]+)$

Kaufmed's pattern may already be what you need, though mine is currently a little stricter with dollar values as it won't allow values with more than 2 decimal places like $.555

Also, I've reduced the 3 requirements:
3.single or double digits Ex:1, 22
4.only alphabets
5.alphabets and numbers

into a single part of the pattern:
[a-zA-Z\d]+
as requirement 5 also covers the cases for requirements 3 and 4.
0
 
KavyaVSAuthor Commented:
I tried this.It is giving unrecognized escape sequence.I added one more \.
^(\$(?=\.?\d)\d*(\.\d\d?)?|\d+(\.\d+)?%|\d\d?|[a-zA-Z\d]+)$


^([a-zA-Z\\d]+)$ it is not taking care of ( opening and ) closing parentheses and spaces.It should be.

^(\\d+(\\.\\d+)?%)$ it is returing true even if there is no decimal.It shouldn't accept with out decimals and numbers after decimal.

Please let me know the changes.
Thanks.
0
 
Terry WoodsIT GuruCommented:
I've changed this bit:
(\\d+(\\.\\d+)?%)
to allow values like:
3.0%
5000.0%
100.0%
0.0%
.00007%
but not values like:
1%
100%

^(\\$(?=\\.?\\d)\\d*(\\.\\d\\d?)?|\\d*\\.\\d+%|\\d\\d?|[a-zA-Z\\d]+)$

Did you want ( and ) characters allowed as well? If so, do they both need to be present and in the expected order? Can there be more than 1 of each?

Perhaps this will be ok:
^(\\$(?=\\.?\\d)\\d*(\\.\\d\\d?)?|\\d*\\.\\d+%|\\d\\d?|[a-zA-Z\\d()]+)$
0
 
KavyaVSAuthor Commented:
I tried the changed expression "^(\\d+(\\.\\d+)?%)$". It is still allowing values like 1% and 2%. But not allowing values like .3%.

^([a-zA-Z\\d]+)$ is not taking care of opening and closing parentheses and spaces between words.
It should accept parentheses,spaces and special characters.

 Please let me know the changes..
Thanks.
0
 
Terry WoodsIT GuruCommented:
KavyaVS, you're using the pattern from before I made the change; apologies if that wasn't clear. Can you please try:
^(\\$(?=\\.?\\d)\\d*(\\.\\d\\d?)?|\\d*\\.\\d+%|\\d\\d?|[a-zA-Z\\d()]+)$ 

Open in new window

0
 
KavyaVSAuthor Commented:
"^([a-zA-Z\\d()]+)$" is returning false for the following sentence.
Daily Assets (including new Assets)
the expression should be like accept the above type sentences and regular text with other special characters.

\\d*\\.\\d+% this one is working fine.

Thanks
0
 
KavyaVSAuthor Commented:
Hi TerryAtOpus,

Thanks for all the changes. They are working fine. I asked another question for Email Validation.
I am using VisualStudio 2010 and C#.

I am using "[^  ]+@bkwsu.org" as regex.is it correct for the following requirement.
The email can be anything before bkwsu.org but the domain should be @bkwsu.org

I tried different options .It is working.
I want to make sure whether it is ok or not.

Please help me in the newly opened question.

Thanks.
0
 
KavyaVSAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.