Reply to Re: best way to validate this data

Your name:

Reply:


Posted by Koncept on 12/23/06 08:48

In article <elmt7n018f9@drn.newsguy.com>, Tom <tom@to.com> wrote:

> On Mon, 11 Dec 2006 20:03:42 -0800, Bosconian wrote...
> >
> >"Benjamin" <musiccomposition@gmail.com> wrote in message
> >news:1165548753.790590.146590@n67g2000cwd.googlegroups.com...
> >>
> >> Bosconian wrote:
> >>> I need a way to validate the followng data with these restrictions:
> >>>
> >>> no leading and trailing white space
> >>> no trailing comma
> >>> double quoted numeric/alpha pairs
> >>> each pair on a separate line
> >>>
> >>> For example:
> >>>
> >>> "1","Peter"
> >>> "2","Paul"
> >>> "3","Mary"
> >>>
> >>> Any help is appreciated.
> >> A regular expression would be great here. Try this:
> >> preg_match($data, "/^(\"\d+\",\"[A-Za-z]+\")+$/")
> >>
> >
> >I tested your pattern, but it only recognizes the first line. I tried
> >(unsuccessfully) inserting a carriage return (\r) after the last quote:
> >
> >preg_match($data, "/^(\"\d+\",\"[A-Za-z]+\"\r)+$/")
> >
> >Any suggestions on how to make it work for multiple lines?
> >
> >
> >
>
> Maybe the extra "+" by the end of line characters "$" is creating a problem.
> You
> already have one or more numbers defined in the first set of quotes and one or
> more letters in the second set, so the "+" outside the parenthesis may not
> match
> up and may not need the parenthesis at all. If the "\r" isn't recognized maybe a
> "\n" would instead.
>
>
> Tom

You might be better of splitting the string and applying the pattern
check to each item in the split parts instead of trying to tackle this
all in one go.

You can capture all the matches using something like this:

<?php
$data = <<<EOF
"1","Peter"
"2","Paul"
"3","Mary"
EOF;
preg_match_all( '/^("\d+","[a-z]+")$/im', $data, $matches );
print_r( $matches );
?>

But this will not inform you if lines were skipped because they did not
validate. I wish I could help more, but I am not one of those regex
geniuses. Your answer probably lies with a combination of PCRE pattern
modifiers if I were to take a guess.

see http://us2.php.net/pcre.pattern.modifiers for more information

Take care.

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация