Solved

# Regular Expression

Posted on 2012-08-13
322 Views
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.
0
Question by:KavyaVS

LVL 74

Expert Comment

For which language or editor?
0

LVL 34

Assisted Solution

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

LVL 74

Assisted Solution

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})

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

Author Comment

I am using C# and VisualStudio 2010.
items such as "\$10" or "3%" are also valid.

Thanks
0

LVL 34

Assisted Solution

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

Author Comment

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

LVL 34

Assisted Solution

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

Author Comment

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

LVL 34

Assisted Solution

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()]+)\$

0

Author Comment

"^([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

LVL 34

Accepted Solution

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() ]+)\$

If you'd like "other special characters" accepted, you'll need to define them.
0

Author Comment

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.

Thanks.
0

Author Closing Comment

Thanks
0

## Featured Post

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…