0

I'm studying computer science and learning about regexps and we have discussed FSM and regular expressions. I have also learned in my hardware class that you can use logical operators not and or to define a large variety of logical and mathematical operations (such as and, nand, xor, adders, etc.) Since many dialects of regex have logical not and logical or, what is the drawback or other problem with using something like:

(taken from a recent homework assignment, design a FSA that accepts only a and b but never in pairs.)

^((^aa*)|(^bb*))*

I know there are better regexps for this example, such as (ab)*|(ba)*, but I'm curious about the theory here.

jwodder
  • 54,758
  • 12
  • 108
  • 124
  • I think theoretical questions like this would be better posted to cs.stackexchange.com. SO is for practical programming questions. – Barmar Jun 06 '16 at 18:27
  • 2
    Also, I think that should be `[^a]a*`. `^` has a different meaning outside `[]`. – Barmar Jun 06 '16 at 18:27
  • This might be a question for http://cs.stackexchange.com/ – rrauenza Jun 06 '16 at 18:28
  • @Barmar: what `[]`? There's only `()` in there. – Marc B Jun 06 '16 at 18:31
  • As others said, this is a rather theoretical question. However, for your particular case, **lookarounds** might be suited in the regex engines that support them. For instance, have a look at this example [`(?<!a)a(?!a)`](https://regex101.com/r/cF3pD0/1) – Jan Jun 06 '16 at 18:33
  • @MarcB Outside square brackets, `^` matches the beginning of the string. When used at the beginning of `[...]` it means to invert the match. – Barmar Jun 06 '16 at 18:34
  • The degenerate case for regex's is backtracking: http://stackoverflow.com/questions/9011592/in-regular-expressions-what-is-a-backtracking-back-referencing and https://www.cs.cf.ac.uk/Dave/PERL/node80.html – rrauenza Jun 06 '16 at 18:40

0 Answers0