-2

I am not using any programming language in particular, rather this is a University regular language assignment I have to do.

To explain what I have to do is basically make an:

  • infinite language
  • Words must contain any number a's and b's, so no single character words
  • words are palindromes to themselves so for example words I should get are "aabbaa","abba", "abbbba", "ababbaabbaba", "bbbaabbb"

How would I approach making this regular expression?

Minvy
  • 9
  • 4
  • 1
    This is very unclear. Does the regex you write need to also be a palindrome, as suggested by the title? If so, please state that in the post. Can you give some examples of words that are accepted/rejected by the regex, and offer a solution attempt? Should the empty string be accepted? How about `a` and `b`, or `bb` (i.e. only one of the two letters are present)? Thanks. – ggorlen Nov 16 '19 at 17:50
  • 1
    Also please read [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions). I'd also think this would be more on-topic at [cs.stackexchange.com](https://cs.stackexchange.com/) (_after_ cleanup/clarification). – ggorlen Nov 16 '19 at 17:57
  • 1
    Here is a perl solution: https://perldoc.perl.org/5.30.0/perlretut.html#Recursive-patterns – Toto Nov 16 '19 at 18:03

1 Answers1

-1

One choice might be then,

ab+ba

or

ab*ba

since single chars are not allowed.

These expressions would simply meet the infinite set and palindrome criteria.

RegEx Demo 2


If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

enter image description here

Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69
  • 4
    This doesn't accept `b` which should be a valid word in the language (or `bab` if the language requires both `a` and `b` to be present, it's unclear). – ggorlen Nov 16 '19 at 17:49
  • 3
    It also matches `bba`, `aaaaaba` and many others which aren't palindromes. – canton7 Nov 16 '19 at 17:52
  • 3
    Maybe we should figure out OP's intent before jumping to conclusions here... The question is ambiguous at best, and seems unanswerable in its current state (is the regex itself supposed to be palindromic or not? Do both `a` and `b` need to be present? etc). – ggorlen Nov 16 '19 at 17:52
  • 1
    The question is very confused, but the bit starting "*for example words I should get are*" appears not to fit with your answer. – canton7 Nov 16 '19 at 18:04
  • 1
    Yes, the _set_ is infinite (the definition of "infinite language"--we can always add one more character to any existing word and expand the language by one) but the strings themselves aren't. We're probably talking about the same thing, but you can have a palindrome with one `a` or one `b`: `aba`, `bab`, `bbabb`, `a`, `b`, etc... – ggorlen Nov 16 '19 at 18:22