Reputation: 41
Can someone help me how to specific pattern for preg_match
function?
Examples:
Can you help me please how to test the string? My pattern is
/^(([a-zA-Z]+)(?! ) \.)+\.$/
I know it's wrong, but i can't figure out it. Thanks
Upvotes: 3
Views: 2243
Reputation: 18545
Check how this fits your needs.
/^(?:[A-Z]+\. ?)+$/i
^
matches start(?:
opens a non-capture group for repetition[A-Z]+
with i
flag matches one or more alphas (lower & upper)\. ?
matches a literal dot followed by an optional space)+
all this once or more until $
endIf you want to disallow space at the end, add negative lookbehind: /^(?:[A-Z]+\. ?)+$(?<! )/i
Upvotes: 3
Reputation: 1680
Try this:
$string = "Ing
Ing.
.Ing.
Xx Yy.
XX. YY.
XX.YY.";
if (preg_match('/^([A-Za-z]{1,}\.[ ]{0,})*/m', $string)) {
// Successful match
} else {
// Match attempt failed
}
Result:
The Regex in detail:
^ Assert position at the beginning of a line (at beginning of the string or after a line break character)
( Match the regular expression below and capture its match into backreference number 1
[A-Za-z] Match a single character present in the list below
A character in the range between “A” and “Z”
A character in the range between “a” and “z”
{1,} Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\. Match the character “.” literally
[ ] Match the character “ ”
{0,} Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Upvotes: -1