Search code examples
phpregexutf-8numberspunctuation

PHP Regexp - if custom punctuation symbols are side-by-side, then regex doesn't match


my regexp

/^[\p{L}\p{N}][\p{L}\p{N} \.,;:\?!-“”‘’"']+$/u

aim of regexp

allow utf-8 characters, numbers, spaces AND custom punctuation to verify article title

these inputs below don't match but I want matching also if punctuation are side-by side? Can you show me the correct form of my regexp? note: Backslash in front of dot and question mark are for escaping attempt. I also tried without escaping. I am not good at regexp. I can only find sub-parts then try to combine. thanks. BR

inputs that don't match

  1. "Selim"!'"':?-
  2. "'
  3. '"
  4. ?!
  5. I also discovered that I can not start with punctuation to a title. example "title" Day doesn't match

Solution

  • change with:

    /^[\p{L}\p{N}“”‘’"'][\p{L}\p{N} .,;:?!\-“”‘’"']*$/u
    

    NB: - must be escaped if it isn't in the first or last position within the character class. But . and ? doesn't need.