?
Solved

Regular Expression

Posted on 2012-08-13
13
Medium Priority
?
337 Views
Last Modified: 2012-08-25
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
Comment
Question by:KavyaVS
  • 6
  • 5
  • 2
13 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38290481
For which language or editor?
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 1668 total points
ID: 38290485
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 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 332 total points
ID: 38290495
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:KavyaVS
ID: 38291791
I am using C# and VisualStudio 2010.
items such as "$10" or "3%" are also valid.

Thanks
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 1668 total points
ID: 38293982
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

by:KavyaVS
ID: 38301218
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 35

Assisted Solution

by:Terry Woods
Terry Woods earned 1668 total points
ID: 38302357
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

by:KavyaVS
ID: 38304657
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 35

Assisted Solution

by:Terry Woods
Terry Woods earned 1668 total points
ID: 38310029
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
 

Author Comment

by:KavyaVS
ID: 38313725
"^([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 35

Accepted Solution

by:
Terry Woods earned 1668 total points
ID: 38313805
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
 

Author Comment

by:KavyaVS
ID: 38325367
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
 

Author Closing Comment

by:KavyaVS
ID: 38333310
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…
Suggested Courses
Course of the Month9 days, 16 hours left to enroll

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question