riding a Regex

Asked

Viewed 104 times

1

I’m picking up a bit here with to assemble the Regex of a pattern I set up, which would be this:

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

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

Here’s the code I put together:

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);
        }
    }

https://dotnetfiddle.net/vhLRaz

1 answer

2


Regex:

This is the 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 the demo no regexstorm.

Explanation

  • (a)(l)(t)(e)(r)(a)(c) - Corresponds literally to alterac
  • (ao|oes) - Corresponds literally to at the or '
  • ( )? - Corresponds to zero or once the space ( )
  • (de)? - Corresponds to zero or once the word of
  • ( ) - Literally corresponds to space ( )
  • (c)(a)(d)(a)(s)(t)(r) - Corresponds literally to **cadastr*
  • (o|os) - Corresponds literally to the or the
  • ( )? - Corresponds to zero or once the space ( )
  • (-)? - Corresponds to zero or once the hyphen -
  • ( )(s)(o)(c)(i)(a) - Corresponds literally to space and society
  • (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 change and registration if there is no, the same occurs with the hyphen.

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

  • Poxa Vl!!! was a small detail !!!

Browser other questions tagged

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