Write a regular expression that is its own palindrome [duplicate] - regex

This question already has answers here:
How to check that a string is a palindrome using regular expressions?
(32 answers)
Closed 3 years ago.
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?

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:

Related

What went wrong with a regular expression? [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
I have a specific regular expression, \Good .+\.\. To my understanding that means, match each pattern that starts with "Good ", then any number of word characters (one or more) and finally end with a dot ('.').
So "Good morning." could is a pattern that this regex is matching, also "Good afternoon.", "Good day.", etc. But somehow it also matches the pattern "Good morning. Good afternoon. Good day." as a whole.
How is this possible?
As #Nick noted, .+ absorbs the final \.. I believe it's an example of a greedy expression where an expression tries to match the longest possible string.

Finding Repeated Patterns using a Regular Expression [duplicate]

This question already has answers here:
How to find overlapping matches with a regexp?
(4 answers)
Closed 3 years ago.
I'm trying to extract the repeated pattern from a string.
For example with something like "112112112112" I would want to end up with "112".
I've been having problems where I either end up with "1" or "112112".
The patterns can be of any size.
Here's an example of the kind of expressions I've been playing around with.
^(.+)(?=\1)
There are repeated patterns with different sizes, if 3 would be desired, for instance, we'd use a quantifier for that, such as:
(.{3})(?=\1)
Demo 1
or
(.{3,5})(?=\1)
Demo 2

RegEx for special characters and decimals except for commas [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
Regex testing for special characters, decimal except for hyphen, commas, alpha-numeric.
Attempt
^(\+|-)?([0-9]+)$/
I'm trying to write a regex to match special characters, decimal except for commas, a hyphen, alpha-numeric.
Welcome!
Maybe, you might just want to list the chars that you wish in a [] and test it. Maybe, a simple expression like this would be desired for you and you could work on it:
^([?!"'~&%$*##0-^#9]+)$
You might want to use this tool and design an expression that you wish, then test it with real samples and maybe change it as you wish.
You can also use an online visualizer to view how your expression would work:

regexp don't match if contain specific digit length [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 5 years ago.
I've tried to make a regular expression that match anything except if contains a 11 digits like 12345678910 so don't match anything
what i have tried
[^\d{11}]
but {11} doesn't work with \d expression
so what i have to do ?
you can use the regex
^(?!.*\d{11}).*$
see the regex101 demo
It's not a very good task for regex to solve actually, because you have to describe every string that doesn't contain 11 consecutive digits.
If possible, I suggest matching a string that does contain 11 consecutive digits, then inverting the success of that match with the language or tool from which you execute this regex.
Depending on your regex flavour, you might also be able to use a negative lookahead such as presented in other answers.
This seemed to work for me using a negative look around:
/^((?!\d{11}).)*$/gm

Regex: Regular expression for algorithmic operations with parentheses [duplicate]

This question already has answers here:
Regular expression for math operations with parentheses
(4 answers)
Closed 2 years ago.
I need help to build a regular expression that accepts the basic arithmetic operations algorithm, but also allows meter operations on any number of parentheses
so far I have this expression:
^([(]*(-)?\d+(\.\d+)?[)]?)([(]?[-+/*%^]?\d+(\.\d+)?[)]*)+
It happens that the above expression accepts me without closing parenthesis or unopened (parentheses must go in pairs).
I show the evidence that I have made, which is in the red box should not accept
http://regexr.com/38r4u
And I hope you can help me,
Thanks.
You cannot parse a recursive structure using a regex. Use a parser instead.