473,378 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Regular expression problem

Hi. Not exactly a regex pro, but usually I know enough to get by. Here
is my problem:

I wrote a regex to edit a decimal number. you're allowed 1,2, or 3
digits before the decimal point, and 0,1,2,or 3digits after the decimal
point. Of course, you can't enter a decimal point without at least a
digit after ("5." is invalid). So here is my regex

pattern=/^\d{1,3}(\.(?=\d))\d{0,3}$/

This works fine for every case except an integer. In other words, it
tests false for entering 5, or 567.
I don't see why it tests false for integers. I'm allowing 1-3 digits
before the decimal point, then a decimal point only if the next
character is a digit (the lookahead clause), and then 0-3 digits after
the decimal point.
I've gotten around this problem with other javascript code around
the regex, but I'd just like to know why this "clean" solution doesn't
work.
Thanks for your help. This is a great group, I've learned so much
from the very knowledgable people here (including much regex stuff!)

Jun 8 '06 #1
6 1398


bruce wrote:
I wrote a regex to edit a decimal number. you're allowed 1,2, or 3
digits before the decimal point, and 0,1,2,or 3digits after the decimal
point. Of course, you can't enter a decimal point without at least a
digit after ("5." is invalid). So here is my regex

pattern=/^\d{1,3}(\.(?=\d))\d{0,3}$/


Can't you simply use e.g.
var pattern = /^\d{1,3}(\.\d{1,3})?$/;
That allows one to three digits followed by nothing or by a decimal
point followed by one to three digits.
Still in real life you usually don't want e.g.
000
which that regular expression would allow.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 8 '06 #2
bruce wrote on 08 jun 2006 in comp.lang.javascript:
Hi. Not exactly a regex pro, but usually I know enough to get by. Here
is my problem:

I wrote a regex to edit a decimal number. you're allowed 1,2, or 3
Edit? It seems you want to test a string?
digits before the decimal point, and 0,1,2,or 3digits after the decimal
point. Of course, you can't enter a decimal point without at least a
digit after ("5." is invalid). So here is my regex

pattern=/^\d{1,3}(\.(?=\d))\d{0,3}$/

This works fine for every case except an integer. In other words, it
tests false for entering 5, or 567.
I don't see why it tests false for integers. I'm allowing 1-3 digits
before the decimal point, then a decimal point only if the next
character is a digit (the lookahead clause), and then 0-3 digits after
the decimal point.
I've gotten around this problem with other javascript code around
the regex, but I'd just like to know why this "clean" solution doesn't
work.
Thanks for your help. This is a great group, I've learned so much
from the very knowledgable people here (including much regex stuff!)


The look-ahead is not supported on all browsers, as I heard whispering.
You probably would not want a leading 0, if >=10

<script type='text/jscript'>

function t(n){
return /^(([1-9]\d{0,2})||\d)(||(\.\d{1,3}))$/.test(n)
}

alert(t('.1')); // false
alert(t('1234')); // false
alert(t('123')); // true
alert(t('012')); // false
alert(t('123.')); // false
alert(t('2.77')); // true
alert(t('0.770')); // true
alert(t('0.7700')); // false

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 8 '06 #3

Evertjan. wrote:
bruce wrote on 08 jun 2006 in comp.lang.javascript:
Hi. Not exactly a regex pro, but usually I know enough to get by. Here
is my problem:

I wrote a regex to edit a decimal number. you're allowed 1,2, or 3


Edit? It seems you want to test a string?
digits before the decimal point, and 0,1,2,or 3digits after the decimal
point. Of course, you can't enter a decimal point without at least a
digit after ("5." is invalid). So here is my regex

pattern=/^\d{1,3}(\.(?=\d))\d{0,3}$/

This works fine for every case except an integer. In other words, it
tests false for entering 5, or 567.
I don't see why it tests false for integers. I'm allowing 1-3 digits
before the decimal point, then a decimal point only if the next
character is a digit (the lookahead clause), and then 0-3 digits after
the decimal point.
I've gotten around this problem with other javascript code around
the regex, but I'd just like to know why this "clean" solution doesn't
work.
Thanks for your help. This is a great group, I've learned so much
from the very knowledgable people here (including much regex stuff!)


The look-ahead is not supported on all browsers, as I heard whispering.
You probably would not want a leading 0, if >=10

<script type='text/jscript'>

function t(n){
return /^(([1-9]\d{0,2})||\d)(||(\.\d{1,3}))$/.test(n)
}

alert(t('.1')); // false
alert(t('1234')); // false
alert(t('123')); // true
alert(t('012')); // false
alert(t('123.')); // false
alert(t('2.77')); // true
alert(t('0.770')); // true
alert(t('0.7700')); // false

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Sorry, i meant to TEST, not edit the entry. I just want true or
false, meaning valid or not valid.

Jun 8 '06 #4
Evertjan. wrote on 08 jun 2006 in comp.lang.javascript:

function t(n){
return /^(([1-9]\d{0,2})||\d)(||(\.\d{1,3}))$/.test(n)
}


Following Martin's better syntax:

function t(n){
return /^(([1-9]\d?\d?)||0)(\.\d{1,3})?$/.test(n)
}

There are many ways to Rome.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 8 '06 #5

Martin Honnen wrote:
bruce wrote:
I wrote a regex to edit a decimal number. you're allowed 1,2, or 3
digits before the decimal point, and 0,1,2,or 3digits after the decimal
point. Of course, you can't enter a decimal point without at least a
digit after ("5." is invalid). So here is my regex

pattern=/^\d{1,3}(\.(?=\d))\d{0,3}$/


Can't you simply use e.g.
var pattern = /^\d{1,3}(\.\d{1,3})?$/;
That allows one to three digits followed by nothing or by a decimal
point followed by one to three digits.
Still in real life you usually don't want e.g.
000
which that regular expression would allow.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Thank you! I didn't even need the lookahead! See, the simplest
solutions are the best. Thanks again.

Jun 8 '06 #6

Evertjan. wrote:
Evertjan. wrote on 08 jun 2006 in comp.lang.javascript:

function t(n){
return /^(([1-9]\d{0,2})||\d)(||(\.\d{1,3}))$/.test(n)
}


Following Martin's better syntax:

function t(n){
return /^(([1-9]\d?\d?)||0)(\.\d{1,3})?$/.test(n)
}

There are many ways to Rome.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Thanks for the complete answer!

Jun 8 '06 #7

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

Similar topics

9
by: Harry | last post by:
Hi there, does anyone know how I can build a regular expression e.g. for the string.search() function on runtime, depending on the content of variables? Should be something like this: var...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
7
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...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
3
by: LordHog | last post by:
Hello all, I am attempting to create a small scripting application to be used during testing. I extract the commands from the script file I was going to tokenize the each line as one of the...
25
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...
5
by: shawnmkramer | last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just hanging and eventually getting a message "Requested Service not found"? I have the following pattern: ^(?<OrgCity>(+)+),...
1
by: sunil | last post by:
Hi, Am writing one C program for one of my module and facing one problem with the regular expression functions provided by the library libgen.h in solaris. In this library we are having two...
1
by: Shawn B. | last post by:
Greetings, I'm using a custom WebBrowser control: http://www.codeproject.com/KB/miscctrl/csEXWB.aspx When I get the DocumentSource of a web page I browsed, and run a regular expression...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.