Simple regular expression required

D

donpro

Hi

I have a form with two input elements where I require the user to
enter an alphanumeric string in the following formats:

1) 9999-9-A-99999
2) AAAAA-99999

Where 9 = any numeric
Where A = any case insensitive letter

The caveat is that the number of numbers and characters in each
section divided by hyphens must be exact except for the last group of
digits after the last hyphen of each input which can be between 1 and
11 digits.

Thanks.
 
M

Martin Honnen

donpro said:
I have a form with two input elements where I require the user to
enter an alphanumeric string in the following formats:

1) 9999-9-A-99999
2) AAAAA-99999

Where 9 = any numeric
Where A = any case insensitive letter

The caveat is that the number of numbers and characters in each
section divided by hyphens must be exact except for the last group of
digits after the last hyphen of each input which can be between 1 and
11 digits.

/^\d{4}-\d-[a-zA-Z]-\d{1,11}$/

/^[a-zA-Z]{5}-\d{1,11}$/

assumes letters simply means a..z, but you could of course put in
additional letters into the square braces.
 
D

donpro

donpro said:
I have a form with two input elements where I require the user to
enter an alphanumeric string in the following formats:
1) 9999-9-A-99999
2) AAAAA-99999
Where 9 = any numeric
Where A = any case insensitive letter
The caveat is that the number of numbers and characters in each
section divided by hyphens must be exact except for the last group of
digits after the last hyphen of each input which can be between 1 and
11 digits.

   /^\d{4}-\d-[a-zA-Z]-\d{1,11}$/

   /^[a-zA-Z]{5}-\d{1,11}$/

assumes letters simply means a..z, but you could of course put in
additional letters into the square braces.

Thanks, but it's not working. Not sure is it's my code. The alert
messages are displayed even when I have the correct pattern. Here's
an extract:

var objectInputBlNumber = document.getElementById('billoflading');
var objectInputBookNumber = document.getElementById('bookingnumber');
var blPattern = '/^\d{4}-\d-[a-zA-Z]-\d{1,11}$/';
var bookPattern = '/^[a-zA-Z]{5}-\d{1,11}$/';

if (!trim(objectInputBlNumber.value).match(blPattern)) {
alert('Please ensure that the Bill of Lading Number is of the
format: "9999-9-A-99999"');
}

if (!trim(objectInputBookNumber.value).match(bookPattern)) {
alert('Please ensure that the Booking Number is of the format:
"AAAAA-99999"');
}

function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}
 
M

Martin Honnen

donpro said:
/^\d{4}-\d-[a-zA-Z]-\d{1,11}$/

/^[a-zA-Z]{5}-\d{1,11}$/

assumes letters simply means a..z, but you could of course put in
additional letters into the square braces.

Thanks, but it's not working. Not sure is it's my code. The alert
messages are displayed even when I have the correct pattern. Here's
an extract:

var objectInputBlNumber = document.getElementById('billoflading');
var objectInputBookNumber = document.getElementById('bookingnumber');
var blPattern = '/^\d{4}-\d-[a-zA-Z]-\d{1,11}$/';
var bookPattern = '/^[a-zA-Z]{5}-\d{1,11}$/';

With JavaScript the // delimits a regular expression literal so throw
those '' quotes away, if you want to define a regular expression use
var blPattern = /^\d{4}-\d-[a-zA-Z]-\d{1,11}$/;
then use the test method e.g.
if (!blPattern.test(objectInputBlNumber.value))
 
D

dipesh

Hi,

I was doing SVG when i came across requirement of using regular
expressions.
I wanted to extract one of the subcomponents in the attribute
transform of elements in SVG.
for example, I wanted to extract only the scale(1.2,3) for the
following

<circle transform="translate(2,3) scale(1.2,3) rotate(2)" >

I used the following regex but i get null assigned to the variable
match.

regex = /[_]*^scale\([^a]+\)$/
match = regex.exec(gT);

Thanking you.
 
L

Lasse Reichstein Nielsen

dipesh said:
I was doing SVG when i came across requirement of using regular
expressions.

Hmm, sounds like a school assignment. For real problems it desn't
usually matter how they are solved, as long as they are.
I wanted to extract one of the subcomponents in the attribute
transform of elements in SVG.
for example, I wanted to extract only the scale(1.2,3) for the
following

<circle transform="translate(2,3) scale(1.2,3) rotate(2)" >

I used the following regex but i get null assigned to the variable
match.

regex = /[_]*^scale\([^a]+\)$/

How did you come up with that regexp? Seems like it was taken from
some other language with a significantly different regexp syntax.

What you would want is
var regex = /\bscale\([^)]+\)/;
It matches the word "scale" followed by an opening parenthesis, some
characters that is not a closing parenthesis, and a closing parenthesis.

/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
dipesh said:
I was doing SVG when i came across requirement of using regular
expressions.
[...]
<circle transform="translate(2,3) scale(1.2,3) rotate(2)" >
[...]

[...]
What you would want is
var regex = /\bscale\([^)]+\)/;
It matches the word "scale" followed by an opening parenthesis, some
characters that is not a closing parenthesis, and a closing parenthesis.

\b is insufficient, though, cf. `className'.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <21411dbd-7443-4b24-a0c7-96c5b43029d2@s1
2g2000yqi.googlegroups.com>, Wed, 17 Jun 2009 01:01:42, dipesh
I used the following regex but i get null assigned to the variable
match.

regex = /[_]*^scale\([^a]+\)$/
match = regex.exec(gT);

General rule : reduce the RegExp until it recognises something which is
there - in this case "scale" is the obvious example. Then add more to
the RegExp until it recognises all that must be recognised to locate the
target. Then add the parentheses needed to capture the required part.

In other words, if something does not work, reduce the problem until
either you know what was wrong or what it left works as you expect.
Then build up. Better, though, to start at the bottom, and make small
changes - then, you generally know where the mistake is, if not why.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Lasse said:
dipesh said:
I was doing SVG when i came across requirement of using regular
expressions.
[...]
<circle transform="translate(2,3) scale(1.2,3) rotate(2)" >
[...]

[...]
What you would want is
var regex = /\bscale\([^)]+\)/;
It matches the word "scale" followed by an opening parenthesis, some
characters that is not a closing parenthesis, and a closing parenthesis.

\b is insufficient, though, cf. `className'.

Can't see what className has to do with this. For the stated purpose
(extracting "scale(1.2,3)" from a string containing other parts of
the same general format), the \b suffices to guarantee that "scale"
is not part of a longer word.

/L
 
D

dipesh

/\bscale\([^)]+\)/ was sufficient but I found /\bscale\([\d.,\s-]+\)/
would prevent invalid characters.
I read the FAQ of the newgroup, I find that I had posted the message
in haste. Sorry about that.
Thank you for the link, there are losts of other resources as well.
\b is insufficient, though, cf. `className'.
I didn't quite getit.



Thomas 'PointedEars' Lahn said:
Lasse said:
I was doing SVG when i came across requirement of using regular
expressions.
[...]
<circle transform="translate(2,3) scale(1.2,3) rotate(2)" >
[...]
[...]
What you would want is
 var regex = /\bscale\([^)]+\)/;
It matches the word "scale" followed by an opening parenthesis, some
characters that is not a closing parenthesis, and a closing parenthesis.
\b is insufficient, though, cf. `className'.

Can't see what className has to do with this. For the stated purpose
(extracting "scale(1.2,3)" from a string containing other parts of
the same general format), the \b suffices to guarantee that "scale"
is not part of a longer word.

/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Thomas 'PointedEars' Lahn said:
Lasse said:
I was doing SVG when i came across requirement of using regular
expressions.
[...]
<circle transform="translate(2,3) scale(1.2,3) rotate(2)" >
[...]
[...]
What you would want is
var regex = /\bscale\([^)]+\)/;
It matches the word "scale" followed by an opening parenthesis, some
characters that is not a closing parenthesis, and a closing parenthesis.
\b is insufficient, though, cf. `className'.

Can't see what className has to do with this.

The (X)HTML `class' attribute value, and the corresponding `className'
property, are defined as a whitespace-separated list, too. We have already
worked out a safer expression for such values before.
For the stated purpose (extracting "scale(1.2,3)" from a string containing
other parts of the same general format), the \b suffices to guarantee that
"scale" is not part of a longer word.

Until another SVG version/variant differs. \b matches a *word boundary*,
within considerable limits (\w != \S).


PointedEars
 
T

Thomas 'PointedEars' Lahn

dipesh said:
/\bscale\([^)]+\)/ was sufficient but I found /\bscale\([\d.,\s-]+\)/
would prevent invalid characters.
I read the FAQ of the newgroup,

Unlikely, see below.
I find that I had posted the message in haste. Sorry about that.
Thank you for the link, there are losts of other resources as well.

I didn't quite getit.

That is unsurprising. You don't even know how to post properly.
[snipped top post]
 
M

Michael J. Ryan

/\bscale\([^)]+\)/ was sufficient but I found /\bscale\([\d.,\s-]+\)/
would prevent invalid characters.
I read the FAQ of the newgroup, I find that I had posted the message
in haste. Sorry about that.
Thank you for the link, there are losts of other resources as well.

I think you want "\." not "." == wildcard.
 
D

dipesh

/\bscale\([^)]+\)/ was sufficient but I found /\bscale\([\d.,\s-]+\)/
would prevent invalid characters.
I read the FAQ of the newgroup, I find that I had posted the message
in haste. Sorry about that.
Thank you for the link, there are losts of other resources as well.

I think you want "\." not "." == wildcard.
I meant /\bscale\([\d\.,\s-]+\)/
Thankx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top