Asked  7 Months ago    Answers:  5   Viewed   39 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

30

You could use Unicode character properties to describe the characters:

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

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

Wednesday, March 31, 2021
 
fillobotto
answered 7 Months ago
22
$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);
Wednesday, March 31, 2021
 
Noob_Programmer
answered 7 Months ago
39

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, May 29, 2021
 
McAn
answered 5 Months ago
48

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

Saturday, May 29, 2021
 
SuperString
answered 5 Months ago
45

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

Thursday, October 14, 2021
 
user2908600
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :