Regular Expression Regex

1

I want to make patterns like this:

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

To be able to register a word, you must be within one of these patterns.

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 out, because anything that is not letter or letter with an accent is replaced by emptiness. I store this exchange in a new string and compare it with 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 with the other standards. I did the test and this:

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

It's accepting to start with space. But you should change the space that is initially empty. Space should only be allowed in between. I do not know how to use those expressions. I believe that someone who knows, will know how to leave my expression.

    
asked by anonymous 05.10.2016 / 17:24

1 answer

3

Try using this regular expression:

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

Explanation of what I put in the most:

  • + determines that what comes immediately before it should appear 1 or more
  • ? optional
  • ' '? space not required
  • \-? hyphen not required

See working at Online Regex .

    
05.10.2016 / 19:30