Viewed   91 times

I’d like to give my users the option to not only fill in letters and numbers, but also “special” letters like the “á”, “é”, etc. However, I do not want them to be able to use symbols like “!”, “@”, "%”, etc.

Is there a way to write a regex to accomplish this? (Preferably without specifying each special letter.)

Now I have:

$reg = '/^[w-]*$/';

 Answers

4

You could use Unicode character properties to describe the characters:

/^[p{L}-]*$/u

p{L} describes the class of Unicode letter characters.

Wednesday, October 26, 2022
4
$test = '<font color="black">0</font><font color="white">1101100001001101</font><font color="black">1</font><font color="white">0110</font>';

echo preg_replace ('~(<font color="white">)([10]*)(</font>)~e', '"\1" . str_repeat(" ", strlen ("\2")) . "\3"', $test);
Sunday, September 25, 2022
4

Thank everybody for help.

My solution based on 'bobbogo' solution. Thank you.

Regular expression:

(?=(XX.*?YY.*?ZZ))(?=(.*ZZ))

Result (from RegexBuggy):

1 XXccYYeeXX_ZZ     XXccYYeeXX_ZZkkYYmmXX_ZZnnXXooYYuuXX_ZZ
2 XX_ZZkkYYmmXX_ZZ      XX_ZZkkYYmmXX_ZZnnXXooYYuuXX_ZZ
3 XX_ZZnnXXooYYuuXX_ZZ  XX_ZZnnXXooYYuuXX_ZZ
4 XXooYYuuXX_ZZ     XXooYYuuXX_ZZ

Possible it can by more optimized? I am not big professional in regex.

Saturday, November 26, 2022
 
parris
 
5

There are couple of problems:

  1. Your regex pattern will also match an input of more than 15 characters.
  2. Your regex will also other non-allowed characters in the middle like @ or # due to use of S

You can fix it by using a negative lookahead to disallow consecutive occurrence of period/hyphen/underscore and remove S from middle of regex that allows any non-space character

^[a-zA-Z0-9](?!.*[_.-]{2})[w.-]{4,13}[a-zA-Z0-9]$

RegEx Demo

Monday, August 15, 2022
1

Use the /u modifier. That will enable Unicode for the regexes. http://php.net/manual/en/reference.pcre.pattern.modifiers.php

Monday, October 17, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :