Regex no Delphi

Asked

Viewed 2,281 times

4

A help with regular expressions in Delphi 10 please. I need to check the following on a string:

  1. Size ranging from a minimum of 6 characters to a maximum of 20 characters;
  2. Character type: a-z or A-Z or 0-9 or _ (character "underline");
  3. 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.

  • @Mariano Truth, I didn’t pay attention to it... I’ll try to improve

  • 1

    @Mariano Look at this: [^\w]|[\w]{21}|^[\w]{6}$

  • @Mariano Your regex worked for positive, mine for negative. Yes, mine invalidates if there are less than 6 and higher than 20

  • @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!

  • 2

    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.

  • Thank you guys. These shapes you have indicated also helped me to apply in other situations.

  • 1

    @danieltakeshi In Delphi, you can use \p{L} for letters.

Show 3 more comments

1 answer

5


  • \w home a-z, A-Z, 0-9 or _.
  • You only have to repeat it 6 to 20 times, that’s it \w{6,20}
  • From the beginning of the string ^ until the end of the string $.

So:

^\w{6,20}$

And in this case, it’s easier to use the method Tregex.Ismatch()

Code:

{$APPTYPE CONSOLE}

uses
    System.RegularExpressions;

var texto: string;

begin
    texto := 'AaaaaaaaBCCcc654_qq';

    if TRegEx.IsMatch(texto, '^\w{6,20}$') then
        begin
            Writeln(texto, ' é válido');
        end
    else
        begin
            Writeln(texto, ' não é válido');
        end;
end.
  • Thank you @Mariano. It worked perfectly. One last question please? The character $ is not a end of line indicator only? What is its function in this Regex?

  • @wBB $ corresponds to the end of the string by default, or can be changed to match the end of the line, using the modifier Multiline, that is, with RegExprModifierM or using (?m) in the expression. The same goes for ^. If you want, you can use \A and \z, which always corresponds to the start / end of the string, regardless of the modifier.

  • @wBB A regular expression corresponds to the text anywhere in the string. Therefore, \w{6,20} can match the first 20 characters of a larger string... ^ and $ correspond the positions start / end of string, and ensure that matches the entire string, that is the size is between 6 and 20.

  • I’m gonna train some Regex to demystify... I didn’t think it was mandatory to use these characters at the beginning and end, so my Regex would never work. Thank you.

  • @wBB It is required only if you want to match the entire string, not with a part of it.

  • Yes... as I was not using these characters, so were being validated things that should not be validated and so I could not fix the problem.

  • @wBB Exact..:-)

  • 1

    Very good @Mariano great explanations, congratulations :D

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.