mounting a RegEx

1

I'm picking up a bit here to mount the Regex of a pattern I put together, which would be this:

ALTERAC[AO,OES] [DE] CADASTRO[S] [-] SOCIAL

What is between [ ] is what can vary. The doubt is that in the words "DE" and "-" may or may not have, so I wanted to ignore them. I have tried in many ways but it is not going out as needed.

Here is the code I put up:

using System;
using System.Text.RegularExpressions;

    public class Program
    {
        public static void Main()
        {
            string texto = "alteracao de cadastro social";
            //string texto = "alteracao de cadastro - social";
            //string texto = "alteracoes de cadastro social";
            //string texto = "alteracao cadastro social";
            //string texto = "alteracao cadastro - social";


            bool passou = (new Regex(@"(a)(l)(t)(e)(r)(a)(c)(ao|oes)( )(de)( )(c)(a)(d)(a)(s)(t)(r)(o|os)( )(-)(s)(o)(c)(i)(a)(l)").IsMatch(texto));


            Console.WriteLine(passou);
        }
    }

link

    
asked by anonymous 05.04.2018 / 16:11

1 answer

2

Regex:

This is Regex: (a)(l)(t)(e)(r)(a)(c)(ao|oes)( )?(de)?( )(c)(a)(d)(a)(s)(t)(r)(o|os)( )?(-)?( )(s)(o)(c)(i)(a)(l|is)

And demo no regexstorm .

Explanation

  • (a)(l)(t)(e)(r)(a)(c) - Corresponds literally to alterac
  • (ao|oes) - Corresponds literally to to or o
  • ( )? - Matches the space ()
  • (de)? - Corresponds to zero or once the word from
  • () - Literally matches the ()
  • (c)(a)(d)(a)(s)(t)(r) - Corresponds literally to ** cadastr *
  • (o|os) - Corresponds literally to the or the
  • ( )? - Matches the space ()
  • (-)? - Corresponds to zero or once the hyphen -
  • ( )(s)(o)(c)(i)(a) - Corresponds literally to space and member
  • (l|is) - Corresponds literally to l or is

Problem

Your problem is that with this logic:% ALTERAC[AO,OES] [DE] CADASTRO[S] [-] SOCIAL There are two spaces between alteration and registration if there is not the same with the hyphen

.

The logic used to fix it is: ALTERAC[AO,OES][ DE] CADASTR[O|OS][ -] SOCIA[L|IS]

    
05.04.2018 / 18:46