MrG
MrG

Reputation: 5287

OR operator in PHP regex (including unicode validation)

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!

Upvotes: 1

Views: 381

Answers (1)

David Schmitt
David Schmitt

Reputation: 59355

\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

Upvotes: 1

Related Questions