4
A help with regular expressions in Delphi 10 please. I need to check the following on a string
:
- Size ranging from a minimum of 6 characters to a maximum of 20 characters;
- Character type: a-z or A-Z or 0-9 or _ (character "underline");
- The allowed characters can be in any position and in any quantity (up to the limit of the string, of course);
Examples
Strings VALID:
AaaaaaaaBCCcc654_qq
1111s123AaBCcc654_qq
____ds4___xx
12345_123
Strings INVALID:
12345-123 (tem hífen)
asdkdn 092834sdfdf (tem espaco em branco)
$123.0 (tem "$" e ".")
I tried things like that, but they didn’t work:
var ret: TMatch;
begin
ret := TRegEx.Match(Edit1.Text, '([a-z]*[A-Z]*[0-9]*[-]?)', [roIgnoreCase]);
or
ret := TRegEx.Match(Edit1.Text, '(\w*)', [roIgnoreCase]);
See if this helps. I’m not very good at regex but I made an effort here:
[^\w]|[\w]{21}
. If either of the two conditions were met, the string is invalid.– Sam
@Mariano Truth, I didn’t pay attention to it... I’ll try to improve
– Sam
@Mariano Look at this:
[^\w]|[\w]{21}|^[\w]{6}$
– Sam
@Mariano Your regex worked for positive, mine for negative. Yes, mine invalidates if there are less than 6 and higher than 20
– Sam
@Mariano actually, instead of
{6}
, would be{5}
:[^\w]|[\w]{21}|^[\w]{5}$
. But I’m totally newba in this rs... I found this my code mt great for little thing. Thanks!– Sam
In special characters, accents are included? For example: valid, accents, etc... If you want to insert special characters, you can do it manually with Regex
^[\wàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð']{6,20}$
and the test demo. But there is an easier way to do this, to use Unicode.– danieltakeshi
Thank you guys. These shapes you have indicated also helped me to apply in other situations.
– wBB
@danieltakeshi In Delphi, you can use
\p{L}
for letters.– Mariano