This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions of Javascript Regular Expression

 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one problem about JavaScript regular expression which does not work as what I want.
User has reqirement as following:
W = Alpha/Numeric (No Blank)
Y = Alpha/Blank
Z = Numeric/Blank

Now user required the field must match mask:WWWWWYZ, which means
The match value for this should be "12345","12345A", or "12345A9"
or "12345 (2 spaces)","12345A (1 space)", or "12345A9"

I translated the mask to the following javaScript regular expression:
"[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]([a-z]|(\s*))([0-9]|(\s*))"
However, this regular expression match value for "1234567", this should not happen.

If you change regular expression to ]"[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]([a-z]|(\s+))([0-9]|(\s+))", then "12345" could not match.

Anyone in this forum can help me with this? This is really urgent for our project, which is cutting off soon.

Thanks in advance.

Sam
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are going to need a ? for the second half to say this can appear one time

...[a-z0-9](([a-z]|(\s+))([0-9]|(\s+)))?

Eric
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric,

Thank you very much for your quick reply.

We have the other several other Charaters to stand for other situation:
A -Alpha no blank
X- A-Z,0-9 and variable blanks,
B- blank,
9- 0-9 numeric only
and
W = Alpha/Numeric (No Blank)
Y = Alpha/Blank
Z = Numeric/Blank


I didn't make myself very clear here.

"WWWWWYZ" is not the user's possible mask.

They may have mask whatever they what.

They may have "YZWWZYW" or "AAAXXBBWYZ".

So I have to match character by character.

Can your solution satisfy all different kinds of combination?

I will try you solution like:
...[a-z0-9]([a-z]|(\s+))?([0-9]|(\s+))?


Thank in advance.

Sam
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric or any expert from this forum,

Can someone help me to solve this problem?

Thanks and regards,

Sam
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,

I already tried
...[a-z0-9]([a-z]|(\s+))?([0-9]|(\s+))?

This reg expression will match
'123456', '1234567' for mask 'WWWWWYZ' that is not what I want.

Any idea why? Or what I should do?

Thanks and regards,

Sam
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34671
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam,
Why do you have to use a regular expression? It seems easier to get the character at each position in the string and compare to the rules for that character.
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank Eric and Jeanne for your reply.

The problem is solved. User has special requirement when "X","Y" and "Z" is at the beginning and at the end.

Regards,
Sam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic