Regular Expression Regex

Asked

Viewed 505 times

1

I want to make patterns that way:

[a-z]+[acentos] || [a-z]+[acentos]+[espaço]+[a-z]+[acentos] || [a-z]+[acentos]+[-]+[a-z]+[acentos]

In order to be able to register a word, it is necessary to be within one of these standards.

For example:

téstê || téstê téstê || téstê-téstê

I did so:

palavra = TextBox1.Text;
Regex regex;
string padrao = @"[^A-Za-záàâãéêíîóõôúûçÁÀÂÃÉÈÍÓÔÕÚÇ']";
regex = new Regex(padrao);
string palavra2 = regex.Replace(palavra, "");
if (palavra == palavra2)
{
    Label1.Text = "Ok!";
}
else {
    padrao = @"[^A-Za-záàâãéêíîóõôúûçÁÀÂÃÉÈÍÓÔÕÚÇ'] [^A-Za-záàâãéêíîóõôúûçÁÀÂÃÉÈÍÓÔÕÚÇ']";

    regex = new Regex(padrao);
    palavra2 = regex.Replace(palavra, "");
    if (palavra == palavra2)
    {
        Label1.Text = "Ok!";
    }
}

The first pattern worked because everything that is not letter or letter with accent is replaced by empty. I store this exchange in a new string and compare it to the previous one, if they are the same, the word followed the pattern. If the first string had a number for example, the new string (which is the default) would be without the number, so the word did not follow the pattern.

The problem is in the other patterns. I took the test and this:

 @"[^A-Za-záàâãéêíîóõôúûçÁÀÂÃÉÈÍÓÔÕÚÇ'] [^A-Za-záàâãéêíîóõôúûçÁÀÂÃÉÈÍÓÔÕÚÇ']";

You are agreeing to start with space. But you should change the space that is in the beginning to void. Space should only be allowed in the middle. I don’t know how to use these expressions. I believe someone who knows, will be able to tell me how to leave my expression.

  • If it was PHP you could do thus, but from what I’ve seen, C# doesn’t have a good Unicode support.

1 answer

3


Try using this regular expression:

[A-Za-zÀ-ú]+ ?\-?[A-Za-zÀ-ú]+

Explanation of what I put the most:

  • + determines that what comes immediately before it shall appear 1 or more
  • ? optional
  • ' '? non-compulsory space
  • \-? hyphen

See working on Online Regex.

  • Thanks, it worked, the "?" solved the problem. I just wanted to understand one thing. Doesn’t this expression obey a sequence? Why did he accept space at first when I put him in the middle?

  • @Heyjoe, how so "does not obey a sequence?" She will accept any sequence that contains letters with or without space and hyphen. If you want to accept space at the beginning just put s in the beginning or s? In the middle accept because of s? between [A-Za-zà-ù]

  • I say in my face, not in yours. In mine, I put the space between the characters, even so he gave match with a space in the beginning, I wanted to know why. So I asked him whether or not he obeys a sequence.

  • In that my expression everything that is not letters he should exchange for empty, but he accepted the space at the beginning, that I did not understand, he did not change the space at the beginning, but should...

  • @Heyjoe, I thought it was mine. I think in your expression you accepted space because when you add a logo after [ you define classes that do not contain certain characters. For example: [ a] recognizes any character, except the.

  • So, read my comment a second before your rsrs see my code there in c#, everything that is not letters at the beginning is replaced by empty, IE, space should be replaced by empty but this did not happen. The problem was solved with the "?" and the "+" you gave me.

  • @Heyjoe, accept because the space is not letter. You have put , [ A-Za-záââéêíííóôúûçALL-CONION'], that is, will give Math in everything that is not letter. Look at that: https://regex101.com/r/MWlmqD/2

  • 1

    Thank you, I do not understand much yet the behavior of these expressions, but using the site you gave me https://regex101.com/r/MWlmqD/2 gave to understand better.

  • "\s? space not mandatory"? I recommend reading What the REGEX shortcut means?

  • @Guilhermelautert, the "Class s is equivalent to class [ t n x0B f r]"

  • @Taisbevalle came to see link I sent? In order to avoid problems if what you are looking for is space then one should use space and not \s.

  • @Guilhermelautert, I saw the link yes.

Show 8 more comments

Browser other questions tagged

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