• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pattern Mattching

 
Joe Brigs
Ranch Hand
Posts: 70
IBM DB2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to check for any number in the 1st 4 positions and ends with a zero in the 5th position, I've tried diffrent ways and searched internet, but I'm just missing something here. Probably very obvious. I'll keep reading maybe I'll get it, but any help is appreciated.

( i.e. 99990 would pass edit , 99993 would fail )

var fundCustID = /^\d{4}\0{1}$/;

if (document.menuForm.applCd.value == "01" ) {
if (fundCustID.test(document.menuForm.custID.value)) {
// just continue its valid
} else {
editError = 'Y';
extServIDError = 'Y';
alert("Failed Edit ");
} }
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the / at the beginning and the end mean?
Is ^ beginning of line and $ end of line? How you you know your number will occupy a whole line?
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\d{4} will match 4 digits, not a digit in the 4th position. But that might have been what you really wanted.
Why have you got a \ before the 0? If you write 0, do you actually need the {1}?

[Edit]Yes, 4 digits was actually what you wanted. sorry, I didn't read your post correctly.[/Edit]
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What does the / at the beginning and the end mean?

That's Perl style regular expressions: the regex itself is between two slashes, followed by optional flags. PHP uses the same syntax.
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't you just use charAt(4) == '0'?
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if ^ is line start and $ is line end, how do you know that is the whole line. Can you change that to \b for word boundary or similar? Remember, in Java™, a \ is an escape character; if you write a String literal, you must escape the \ to \\.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mentioned PHP and Perl; are you using Java™ to test this regex?
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, I can't bring myself to use the TM with a straight face. But it probably is worth pointing out that this is a Java forum, and the code shown is definitely not Java. Perhaps it would be helpful to tell readers what language you are using? I'm guessing it's JavaScript, which has remarkably little to do with Java.
 
Joe Brigs
Ranch Hand
Posts: 70
IBM DB2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To All , Thanks for the help. Yes its Javascript and I will try to post to the correct board in the future. I'm sure theres diffrent ways to do this but I solved my pattern by defining "var flexCustID = /^2\d{4}0000000000$/;" and then using it in "if (flexCustID.test(document.menuForm.custID.value)) { do this do that ..... "

Also I found the the /^\ etc. on page 322 of the Head First JavaScript book. Which explains Reg Expressions but could do better. Thanks Again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic