tags:

views:

99

answers:

1

Thanks to S. Gehrig's answer in the initial question I've got a regex which works fine and validates a variable based on the Letter property (except Chinese, but that's another topic :):

if (preg_match('/^\p{L}+$/u', $input)) {
    // OK
}

Unfortunately I can't extend it to support to support numbers respective question/exclamation & co. My experiments included:

'/^[\p{L}]|[0-9]|[\n]|[']|[\?]|[\!]|[\.]|[\,]+$/u'
'/^[\p{L}+]|[0-9]|[\n]|[']|[\?]|[\!]|[\.]|[\,]$/u'
'/^[\p{L}+]|[0-9]|[\n]|[']|[\?]|[\!]|[\.]|[\,]$/u'

What is the correct regex? Please point me in the right direction.

Many, many thanks!

+1  A: 

\p{L}+ is already "non-empty string of \p{L}'s". [] on the other hand indicate "one of", thus depending on your actual requirements, either of this should work:

Any (positive, non-zero) number of the specified characters in sequence:

/^[\p{L}0-9\n'?!.,]+$/u

Either a sequence of \p{L}s or a sequence of mixed [0-9\n'?!.,]:

/^(\p{L}+|[0-9\n'?!.,]+)$/u

Either a sequence of \p{L}s or exactly one of [0-9\n'?!.,]:

/^(\p{L}+|[0-9\n'?!.,])$/u
David Schmitt
You can put the \p{L} inside the character class along with the literal characters.
Alan Moore
thanks for the hint. I'm not particularly firm with php's regexes.
David Schmitt
Attention! Your two regular expressions are NOT equivalent. `/^foo|bar$/` is equal to `/(?:^foo)|(?:bar$)/` and `/(?:^foo|bar$)/` but not equal to `/^(?:foo|bar)$/`!
Gumbo
@Gumbo: good catch! fixed.
David Schmitt
Tkx a lot David, it's working perfectly fine. Only ' seems a bit strange, because it's not allowed even if I add i.e. with preg_match("/^[\p{L}0-9 _.\-']+$/u", $value)
MrG