473,509 Members | 2,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular expressions

Hello to everyone,

Yesterday I tried long time to make a validation for a number format
input field.
As I am using Regex in Java I wrote following expression:

String pattern =
"((\\d+)((\\.)(\\d+)(\\,(\\d)+)?)?)|((\\d+)((\\,)( \\d+)(\\.(\\d)+)?)?)";

So the sequence of the chars of the input field could be?

123 or
123.89 or
123.89,99 or
123,89
123,99.88

I used the function search() but I wasn't able to get the same result as
I get in Java.

Any Hints?
Thanks

Markus
Mar 24 '06 #1
5 1428
Markus Innerebner wrote on 24 mrt 2006 in comp.lang.javascript:
Hello to everyone,

Yesterday I tried long time to make a validation for a number format
input field.
As I am using Regex in Java I wrote following expression:

String pattern =
"((\\d+)((\\.)(\\d+)(\\,(\\d)+)?)?)|((\\d+)((\\,)( \\d+)(\\.(\\d)+)?)?)";

So the sequence of the chars of the input field could be?

123 or
123.89 or
123.89,99 or
123,89
123,99.88

I used the function search() but I wasn't able to get the same result as
I get in Java.

Any Hints?


Better ask a Java NG, this NG is about Javascript.

If you are talking javascript show your code.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 24 '06 #2

"Markus Innerebner" <ma****@innerebner.net> wrote in message
news:44***********************@reader2.news.tin.it ...
Hello to everyone,

Yesterday I tried long time to make a validation for a number format input
field.
As I am using Regex in Java I wrote following expression:

String pattern =
"((\\d+)((\\.)(\\d+)(\\,(\\d)+)?)?)|((\\d+)((\\,)( \\d+)(\\.(\\d)+)?)?)";

So the sequence of the chars of the input field could be?

123 or
123.89 or
123.89,99 or
123,89
123,99.88

I used the function search() but I wasn't able to get the same result as I
get in Java.

Any Hints?
Thanks

Markus


I can't decipher yours (just lazy) but I use:

/^((\d{1,3}(,\d{0,3})*)|\d+)(\.\d*)?$/

in JavaScript.

Vic
Mar 24 '06 #3
Markus Innerebner <ma****@innerebner.net> writes:
Yesterday I tried long time to make a validation for a number format
input field.
What are the valid formats? Examples rarely cover the entire range
of valid values.
As I am using Regex in Java I wrote following expression:

String pattern =
"((\\d+)((\\.)(\\d+)(\\,(\\d)+)?)?)|((\\d+)((\\,)( \\d+)(\\.(\\d)+)?)?)";
Ick. Completely unreadable. Let's try to convert it to a RegExp literal
and see if it can be reduced further. I can see "\\," where the escape
isn't necessay:

/((\d+)((\.)(\d+)(,(\d)+)?)?)|((\d+(,)(\d+)(\.(\d)+ )?)?)/

So it's two parts:
((\d+)((\.)(\d+)(,(\d)+)?)?)
and
((\d+(,)(\d+)(\.(\d)+)?)?)

It sounds unlikely that you want to capture all these sub-parts. Or at
least, the (\d)+ should probably be (\d+) if you want to use it for
something.

Let's remove unnecessary brackets - they're only necessary when you
use it, not for unerstanding :). That gives:
\d+(\.\d+(,\d+)?)?
and
(\d+,\d+(\.\d+)?)?

I.e.
1) some digits optionally followed by (a period and some digits,
optionally followed by a comma and some digits)
or
2) nothing, or some digits a comma and some digits, optionally
followed by a dot and some digits.

So, it appears that the format is:
zero or more digits, optionally separated by a comma and/or a period,
i.e., comma and period cannot be at the ends or next to each other.
Is this correct?
So the sequence of the chars of the input field could be?

123 or
123.89 or
123.89,99 or
123,89
123,99.88

I used the function search() but I wasn't able to get the same result
as I get in Java.
What results did you get here, and what results do you get in Java?

Notice that your RegExp isn't anchored, so the default is to test
whether it can match any substring of the tested string. Since it
acceptes the empty string, this is always the case.
Any Hints?


Specify, very precisely, which strings should be accepted.

Consider whether doing it all in one RegExp is better than
doing it in more steps, or in code, e.g.:

function validateString(string) {
if (string == "") { return true; }
if (/,.*,/.test(string)) { return false; }
if (/\..*\./.test(string)) { return false; }
return /^\d+([.,]\d+){0,2}$/.test(string);
}
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Mar 24 '06 #4

Hello Lasse (and to the other repliers),
First of all thanks for your answers.
/((\d+)((\.)(\d+)(,(\d)+)?)?)|((\d+(,)(\d+)(\.(\d)+ )?)?)/
this is that what i did at the begin. It's better to remove the brackets
and split it in two parts, because it's better readable
So it's two parts:
((\d+)((\.)(\d+)(,(\d)+)?)?)
and
((\d+(,)(\d+)(\.(\d)+)?)?)

It sounds unlikely that you want to capture all these sub-parts. Or at
least, the (\d)+ should probably be (\d+) if you want to use it for
something.

Let's remove unnecessary brackets - they're only necessary when you
use it, not for unerstanding :). That gives:
\d+(\.\d+(,\d+)?)?
and
(\d+,\d+(\.\d+)?)?

I.e.
1) some digits optionally followed by (a period and some digits,
optionally followed by a comma and some digits)
or
2) nothing, or some digits a comma and some digits, optionally
followed by a dot and some digits.

So, it appears that the format is:
zero or more digits, optionally separated by a comma and/or a period,
i.e., comma and period cannot be at the ends or next to each other.
Is this correct? This is exactly that what i want. What was missing here (and in my code
too) are the symbols ^ at the begin and $ at the end, so that other
characters are excluded.

Specify, very precisely, which strings should be accepted.

Consider whether doing it all in one RegExp is better than
doing it in more steps, or in code, e.g.:

function validateString(string) {
if (string == "") { return true; }
if (/,.*,/.test(string)) { return false; }
if (/\..*\./.test(string)) { return false; }
return /^\d+([.,]\d+){0,2}$/.test(string);
}


This is a good idea too! Probally I will use something like that.
Now it's clear for me that r.e. in Java are differently than r.e. in
javascript (or perl).
I forget to set the chars '^' on the begin and '$' at the end.

example:

in java: \\d+ can be: 1, 123, but not 12abc
in javascript: /\d+/ can be: 1, 12, and also 12abc, just look if
the string contains a number
so I changed to: /^\d+$/ and this r.e. returns the same result as my
r.e. of java.

Anyway, thanks for helping be

best regards

Markus
Mar 25 '06 #5
Markus Innerebner wrote:
Now it's clear for me that r.e. in Java are differently than r.e. in
javascript (or perl).
They are different, but in a different way than you think.
I forget to set the chars '^' on the begin and '$' at the end.

example:

in java: \\d+ can be: 1, 123, but not 12abc
You have to escape the backslash (`\') in the Java String literal,
because it starts an escape sequence. This is not different in
ECMAScript-conforming String literals.
in javascript: /\d+/ can be: 1, 12, and also 12abc, just look if
the string contains a number


If you used `new RegExp("\\d+")' in an ECMAScript implementation, you could
use the Java String literal verbatim. The issue here is instead that Java
does not appear to have a Regular Expression literal syntax, while
implementations of ECMAScript, such as JavaScript and JScript, have one.

Using RegExp literal syntax avoids escaping the backslash again and makes
Regular Expressions easier readable. Another advantage is that the RegExp
object is created only once per expression because it is created before
execution. However, it also introduces a dependency on JavaScript 1.2 (NN
4.0, June 1997 CE), JScript 3.0 (IE 3.0, October 1997 CE), or an ECMAScript
3 (December 1999 CE) implementation, and has the drawback that it cannot
span lines, which is possible with the RegExp() constructor (through
string concatenation or joining of an array of string values).
PointedEars
Mar 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2418
by: Michael McGarry | last post by:
Hi, I am horrible with Regular Expressions, can anyone recommend a book on it? Also I am trying to parse the following string to extract the number after load average. ".... load average:...
1
4155
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
2
4995
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
4
5141
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
7
3794
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
3
3008
by: a | last post by:
I'm a newbie needing to use some Regular Expressions in PHP. Can I safely use the results of my tests using 'The Regex Coach' (http://www.weitz.de/regex-coach/index.html) Are the Regular...
25
5130
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
4362
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
13
7458
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
12
2438
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.