This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Pattern Mattching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Pattern Mattching " Watch "Pattern Mattching " New topic
Author

Pattern Mattching

Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 60

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

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Oct 13, 2005
Posts: 38045
    
  22
\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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
Couldn't you just use charAt(4) == '0'?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Oct 13, 2005
Posts: 38045
    
  22
You mentioned PHP and Perl; are you using Java™ to test this regex?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3001
    
    9
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

Joined: Jun 06, 2011
Posts: 60

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pattern Mattching
 
Similar Threads
Array trouble
opneing the same aplication in diferent tabs
Information about OCPJWCD 6 / SCWCD 6 [CX-310-085]
Help understanding output with a++
this roundup question trips me up every time