By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,722 Members | 1,904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,722 IT Pros & Developers. It's quick & easy.

regular expression for comma-delimited pairs

P: n/a
I have a textarea form field for inputting (or pasting) pairs of data.

I need a regular expression pattern to validate each line for the following

double quote
number
double quote
comma
double quote
alpha string
double quote
carriage return

The following comes close, but doesn't check for a carriage return at the
end of each line:

^"([0-9]?)+"([,]\s?\"([A-Za-z0-9]+)")*$

For example the following would return true:

"1","John"
"2","Paul"
"3","George"
"4","Ringo"
Any suggestions would be appreciated.
Feb 13 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Bosconian wrote:
I have a textarea form field for inputting (or pasting) pairs of data.

I need a regular expression pattern to validate each line for the following

double quote
number
double quote
comma
double quote
alpha string
double quote
carriage return

The following comes close, but doesn't check for a carriage return at the
end of each line:

^"([0-9]?)+"([,]\s?\"([A-Za-z0-9]+)")*$

For example the following would return true:

"1","John"
"2","Paul"
"3","George"
"4","Ringo"
Any suggestions would be appreciated.

Try the /s modifier. Perhaps a bit "tipsy" to examine the regex in depth.

Curtis
Feb 13 '07 #2

P: n/a
On 13 Feb, 04:06, "Bosconian" <nob...@nowhere.comwrote:
I have a textarea form field for inputting (or pasting) pairs of data.

I need a regular expression pattern to validate each line for the following

double quote
number
double quote
comma
double quote
alpha string
double quote
carriage return

The following comes close, but doesn't check for a carriage return at the
end of each line:

^"([0-9]?)+"([,]\s?\"([A-Za-z0-9]+)")*$

For example the following would return true:

"1","John"
"2","Paul"
"3","George"
"4","Ringo"

Any suggestions would be appreciated.
The ? following [0-9] means that
"","John" will also be matched.
Also, the * before the $ means that
"1" will be matched
So at the very least you want
^"[0-9]+"([,]\s?\"([A-Za-z0-9]+)")$
Since a $ means "the end of the line" and since a carriage return
signifies the end of a line, his should do. Or do you want to ensure
that there is a carriage return at theend, even if there is only one
line?

Feb 13 '07 #3

P: n/a
"Captain Paralytic" <pa**********@yahoo.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...
On 13 Feb, 04:06, "Bosconian" <nob...@nowhere.comwrote:
>I have a textarea form field for inputting (or pasting) pairs of data.

I need a regular expression pattern to validate each line for the
following

double quote
number
double quote
comma
double quote
alpha string
double quote
carriage return

The following comes close, but doesn't check for a carriage return at the
end of each line:

^"([0-9]?)+"([,]\s?\"([A-Za-z0-9]+)")*$

For example the following would return true:

"1","John"
"2","Paul"
"3","George"
"4","Ringo"

Any suggestions would be appreciated.

The ? following [0-9] means that
"","John" will also be matched.
Also, the * before the $ means that
"1" will be matched
So at the very least you want
^"[0-9]+"([,]\s?\"([A-Za-z0-9]+)")$
Since a $ means "the end of the line" and since a carriage return
signifies the end of a line, his should do. Or do you want to ensure
that there is a carriage return at theend, even if there is only one
line?
Thank you for your response and for the pattern corrections.

A trailing carriage return on the last line would be unnecessary and ignored
if present. However, any preceding pairs must have a carriage return at the
end of each line. Where in the pattern is this mandated?

I don't have the opportunity to use regular expressions consistency, but I
am anxious to learn the syntax and am grateful for your instruction.
Feb 13 '07 #4

P: n/a
On 13 Feb, 13:36, "Bosconian" <nob...@nowhere.comwrote:
"Captain Paralytic" <paul_laut...@yahoo.comwrote in message

news:11**********************@s48g2000cws.googlegr oups.com...


On 13 Feb, 04:06, "Bosconian" <nob...@nowhere.comwrote:
I have a textarea form field for inputting (or pasting) pairs of data.
I need a regular expression pattern to validate each line for the
following
double quote
number
double quote
comma
double quote
alpha string
double quote
carriage return
The following comes close, but doesn't check for a carriage return at the
end of each line:
^"([0-9]?)+"([,]\s?\"([A-Za-z0-9]+)")*$
For example the following would return true:
"1","John"
"2","Paul"
"3","George"
"4","Ringo"
Any suggestions would be appreciated.
The ? following [0-9] means that
"","John" will also be matched.
Also, the * before the $ means that
"1" will be matched
So at the very least you want
^"[0-9]+"([,]\s?\"([A-Za-z0-9]+)")$
Since a $ means "the end of the line" and since a carriage return
signifies the end of a line, his should do. Or do you want to ensure
that there is a carriage return at theend, even if there is only one
line?

Thank you for your response and for the pattern corrections.

A trailing carriage return on the last line would be unnecessary and ignored
if present. However, any preceding pairs must have a carriage return at the
end of each line. Where in the pattern is this mandated?

I don't have the opportunity to use regular expressions consistency, but I
am anxious to learn the syntax and am grateful for your instruction.- Hide quoted text -

- Show quoted text -
If you are treating each line as a separate string, then the $ will
indicate this. If you are treating all the lines as a single string
then I need to ask, is it only a carriage return that you will have at
the end of each line and all you want to check for? Sometimes lines
can be ended by a carriage return and a new line (\r\n) \nd sometimes
by a new line only (\n). It is unusual for a line to have only a
carriage return on its own (\r) as, on older printers, this would lead
to the next line overtyping the previous one.

Feb 13 '07 #5

P: n/a
"Captain Paralytic" <pa**********@yahoo.comwrote in message
news:11*********************@q2g2000cwa.googlegrou ps.com...
On 13 Feb, 13:36, "Bosconian" <nob...@nowhere.comwrote:
>"Captain Paralytic" <paul_laut...@yahoo.comwrote in message

news:11**********************@s48g2000cws.googleg roups.com...


On 13 Feb, 04:06, "Bosconian" <nob...@nowhere.comwrote:
I have a textarea form field for inputting (or pasting) pairs of data.
>I need a regular expression pattern to validate each line for the
following
>double quote
number
double quote
comma
double quote
alpha string
double quote
carriage return
>The following comes close, but doesn't check for a carriage return at
the
end of each line:
>^"([0-9]?)+"([,]\s?\"([A-Za-z0-9]+)")*$
>For example the following would return true:
>"1","John"
"2","Paul"
"3","George"
"4","Ringo"
>Any suggestions would be appreciated.
The ? following [0-9] means that
"","John" will also be matched.
Also, the * before the $ means that
"1" will be matched
So at the very least you want
^"[0-9]+"([,]\s?\"([A-Za-z0-9]+)")$
Since a $ means "the end of the line" and since a carriage return
signifies the end of a line, his should do. Or do you want to ensure
that there is a carriage return at theend, even if there is only one
line?

Thank you for your response and for the pattern corrections.

A trailing carriage return on the last line would be unnecessary and
ignored
if present. However, any preceding pairs must have a carriage return at
the
end of each line. Where in the pattern is this mandated?

I don't have the opportunity to use regular expressions consistency, but
I
am anxious to learn the syntax and am grateful for your instruction.-
Hide quoted text -

- Show quoted text -

If you are treating each line as a separate string, then the $ will
indicate this. If you are treating all the lines as a single string
then I need to ask, is it only a carriage return that you will have at
the end of each line and all you want to check for? Sometimes lines
can be ended by a carriage return and a new line (\r\n) \nd sometimes
by a new line only (\n). It is unusual for a line to have only a
carriage return on its own (\r) as, on older printers, this would lead
to the next line overtyping the previous one.
Yes, I assume we're talking about \n or \r\n.

Incidentally, I would like to support several methods of entry. My idea is
to have the user select a radio button for the format. For example,
optionally the user could input

1,John
2,Paul
3,George
4,Ringo

1,John,2,Paul,3,George,4,Ringo

I eventually want it to work the same was as phpMyAdmin's import feature.
However, this will be used by non-tech savvy users so validation will play
an important part.

Again, thanks for your help.
Feb 13 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.