• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pattern Mattching

 
Ranch Hand
Posts: 70
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ");
} }
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Couldn't you just use charAt(4) == '0'?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mentioned PHP and Perl; are you using Java™ to test this regex?
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic