Regular Expression (consecutive 1s and 0s) - regex

Hey I'm supposed to develop a regular expression for a binary string that has no consecutive 0s and no consecutive 1s. However this question is proving quite tricky. I'm not quite sure how to approach it as is.
If anyone could help that'd be great! This is new to me.

You're basically looking for alternating digits, the string:
...01010101010101...
but one that doesn't go infinitely in either direction.
That would be an optional 0 followed by any number of 10 sets followed by an optional 1:
^0?(10)*1?$
The (10)* (group) gives you as many of the alternating digits as you need and the optional edge characters allow you to start/stop with a half-group.
Keep in mind that also allows an empty string which may not be what you want, though you could argue that's still a binary string with no consecutive identical digits. If you need it to have a length of at least one, you can do that with a more complicated "or" regex like:
^(0(10)*1?)|(1(01)*0?)$
which makes the first digit (either 1 or 0) non-optional and adjusts the following sequences accordingly for the two cases.
But a simpler solution may be better if it's allowed - just ensure it has a length greater than zero before doing the regex check.

Related

What is the language accepted by this NFA? Can you express it in English and with regular expression?

I have been trying to solve this NFA, this below it is the best I could come up with. I have hard time describing in English the language it produces, can someone help me to understand better?
Regular expression
(0+11)*10(0+1(0+11)10)
The automata is not deterministic because there are 3 transactions coming out of P
It does not accept words ending with 0 and an even number of 1. It must have at least one 1 and one zero. Sequences of odd numbers of 1s followed by zero. Or even number of 1s ending with a 0 if there is at least one 10 preceding the series of 1.
Later I have decided that this description is better.
Accept all strings ending with 0 and an odd number of 1, it does not accept strings ending with 1. It will not accept strings ending with 0 and even number of 1.
Accepted words:
1110
1001100110
11010
Not accepted
111001
You can see that you only have 0s going into the accepting state, which means the pattern has to end with a 0.
You also see that you need to have at least one 1, otherwise you will be stuck in the starting state.
Finally, you see that going from the starting state if you get an even number of 1s, you will end up in the start state again.
So, in other words, accepted patterns need to contain an odd number of 1s and end with a 0.
The simplest regular expression I can think of is:
0*(10*10*)*10+
The ending is pretty clear: you need a 1 followed by at least one 0 (so 10+).
The beginning should be quite clear too: you should be able to have as many 0s as you like in the beginning, thus 0*.
Now what remains is (10*10*)* which is 10*10* repeated an arbitrary number of times.
What this represents is any pattern with an even number of 1s which starts with a 1 (the fact that we have 0* just before that ensures that the global expression also covers strings that don't start with a 1).
Note that 10*10* contains exactly two 1s, so no matter how many times you repeat this pattern you will always have an even number of ones.
But how do we know that any string with an even number of 1s would satisfy this pattern?
We can prove this inductively.
A string with no 1s at all will match this expression (if we consider the 0* bit at the beginning) so we have our base case covered.
A string with a positive even number of ones can always be split into a prefix with just two 1s and a suffix with an even number of 1s (this even number may itself be 0).
So what is the expression for a string that contains exactly two 1s?
It's 0*10*10* or 0* followed by 10*10*.
So that's it - our pattern works for an string without any 1s and assuming it works for some even number of 1s we showed that it will work for two additional 1s too. That's basically the entire inductive proof.
A quick note to clarify why we only have 0* once at the beginning:
What happens when you have 0*10*10* followed by another 0*10*10*?
That's right - you get 0*10*10*0*10*10* but since 0*0* is equivalent to 0* we can simply the expression and only have a single 0* at the beginning, omitting it from the repeated expression.

Regular Expression Exactly Two 1's

I'm fairly new to using regular expressions and I've been stuck on how to make a regular expression that accepts Binary strings that contain exactly two 1's and an odd number of zeros. I have an idea of the odd zeros part 1*01*(01*01*)* but I'm not sure how to incorporate it into the exactly two 1's part.
from my lecture they said the format was the characters 0* 1 0* 1 0* ?
Probably there is a much more efficient way, but one possibility would be:
use (00)* to include any even number of zeros
add the two ones by hand -> (00)*1(00)*1(00)*
and then go through the four cases of additional zeros using OR so that the total number is odd: one additional 0 in front, in the middle or in the back or one in all of those locations
So the final regex could look like this:
(0(00)*1(00)*1(00)*)|((00)*1(00)*01(00)*)|((00)*1(00)*1(00)*0)|((00)0*1(00)0*1(00)*0)

How do I convert language set notation to regular expressions?

I have this following questing in regular expression and I just can't get my head around these kind of problems.
L1 = { 0n1m | n≥3 ∧ m is odd }
How would I write a regular expression for this sort of problem when the alphabet is {0,1}.
What's the answer?
The regular expression for your example is:
000+1(11)*1
So what does this do?
The first two characters, 00, are literal zeros. This is going to be important for the next point
The second two characters, 0+, mean "at least one zero, no upper bound". These first four characters satisfy the first condition, which is that we have at least three zeros.
The next character, 1, is a literal one. Since we need to have an odd number of ones, this is the smallest number we're allowed to have
The last-but-one characters, (11), represent a logical grouping of two literal ones, and the ending * says to match this grouping zero or more times. Since we always have at least one 1, we'll always match an odd number. So we're done.
How'd I get that?
The key is knowing regular expression syntax. I happen to have quite a bit of experience in it, but this website helped me to verify.
Once you know the basic building blocks of regex, you need to break down your problem into what you can represent.
For example, regex allows us to specify a lower AND upper bound for matching (the {x,y} syntax), but doesn't allow to specify just a lower bound ({x} will match exactly x times). So I knew I would have to use either + or * to specify the zeros, as those are the only specifiers that permit an infinite number of matches. I also knew that it didn't make sense to apply those modifiers to a group; the restriction that we must have at least 3 zeroes doesn't imply that we must have a multiple of three, for example, so (000)+ was out. I had to apply the modifier to only one character, which meant I had to match a few literals first. 000 guarantees matching exactly three 0s, and 0* (Final expression 0000*) does exactly what I want, and then I condensed that to the equivalent 000+.
For the second condition, I had to think about what an odd number is. By definition, an odd number can be expressed by 2*k + 1, where k is an integer. So I had to match one 1 (Hence the literal 1), and some number of the substring 11. That led me to the group, and then the *. On a slightly different problem, you could write 1(11)+ to match any odd number of ones, and at least 3.
1 A colleague of mine pointed out to me that the + operator isn't technically part of the formal definition of regular expressions. If this is an academic question rather than a programming one, you might find the 0000* version more helpful. In that case, the final string would be 0000*1(11)*

how do i write the regex for a limited range repetition

I am creating a form whereby the users can input a multi-value in a limited range
I am having trouble repeating the range as shown below, do i have to validate the comma as well and can i have the full regular expression solution for this?
I am allowing the user to input the value multiple times for a limited range of 0-1000 for 64 times or less
the input can be as follow:
1000,0,100,123,10,23,56,654,981
and here's my current regular expression for the range
(^(?:[0-9]|[1-9][0-9]|[1-9][0-9]{2}|1000)$)
Short answer:
^((1000|\d{1,3})(,|$)){1,64}
(Assuming you don't mind leading zeros. If you do, then change \d{1,3} to the more complex ([1-9][0-9]|[1-9])?[0-9].)
Long version:
We want to match (numbers in the range 0-1000) (repeated 1-64 times).
The first part can be done with (1000|\d{3}) (with the caveat noted above about leading zeros).
For the second part, we use a handy trick to do the comma-separation aspect: we say that each number must either be followed by a comma or the end of the string.
Note that there is a small weakness to this approach: it accepts trailing commas, e.g. 1,2,3, matches. If you're not okay, you can adapt by just adding, but it'll make the pattern longer:
^((1000|\d{1,3}),){0,63}(1000|\d{1,3})$
Note that I use an explicit {0,63} but many regex variants will accept the short form {,63} as well.
Also note that regex might not be the best solution for this - it might be better to just split the input string on commas and then iterate though the pieces, validating that each one is a number from 0-1000 and there are 64 or fewer pieces.

RegEx Numeric Check in Range?

I'm new to StackOverflow, so please let me know if there is a better way to ask the following question.
I need to create a regular expression that detects whether a field in the database is numeric, and if it is numeric does it fall within a valid range (i.e. 1-50). I've tried [1-50], which works except for the instances where a single digit number is preceded by a 0 (i.e. 06). 06 should still be considered a valid number, since I can later convert that to a number.
I really appreciate your help! I'm trying to learn more about regular expressions, and have been learning all I can from: www.regular-expressions.info. If you guys have recommendations of other sites to bone up on this stuff I would appreciate it!
Try this
^(0?[1-9])|([1-4][0-9])|(50)$
The idea of this regex is to break the problem down into cases
0?[1-9] takes care of the single digit case allowing for an optional preceeding 0
[1-4][0-9] takes care of all numbers from 10 to 49. This also allwows for a preceeding 0 on a single digit
50 takes care of 50
Regular expressions work on characters (in this case digits), not numbers. You need to have a separate pattern for each number of digits in your pattern, and combine them with | (the OR operator) like the other answers have suggested. However, consider just checking if the text is numeric with a regular expression (like [0-9]+) and then converting to an integer and checking the integer is within range.
You can't easily do range checking with regular expressions. You can -- with some work -- develop a pattern that recognizes a numeric range, but it's usually quite complex, and difficult to modify for a slightly different range.
You're better off breaking this into two parts.
Recognize the number pattern (^\d+$).
Check the range of that number in an application program.
^0?[1-50]{1,2}$