Are regular expressions (0*1*)* and (0 + 1)* same? [closed] - regex

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I was solving exercise problems in my textbook, and I wondered that whether (0*1*)* and (0 + 1)* are same. I think they are same, but I have no idea how to prove it. Are they same regular expressions?

No, they are not the same:
0*1* matches any number of zeroes (including none) followed by any number of ones (including none). This is then repeated any number of times (including none) via ()*
0 + 1 matches a mandatory single zero, then at least one or more spaces, then one more space, then a mandatory single one. Again, this is repeated any number of times via ()* like before.
So, the two match very different things the first one will match input like 000000 or 1111 because it has (essentially) an optional zero or one, so an input that only has a single of these is correct but not according to the second regex. While the second one will match input that has at least two or more spaces between the mandatory 0 and 1 characters but the first regex will reject that, since it does not allow spaces.

No they aren't. The first on will for example match 0011, but the second one won't.
You can check how they behave in: https://regex101.com/

I think you meant (0+1)*instead of (0 + 1)* (mind the spaces, they are meaningful when dealing with regex).
To answer your question, no. They are not the same.
The + quantifier means "one or more" (it does not mean "or" like you might think), so the (0+1)* regex will match 01 but not 10, whereas the (0*1*)* regex will match both.

Related

Regex that does not repeat the same character 2 times in a row [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to make a regex to validate an amount in which it only accepts numbers, points (.) Optional, 1 comma (,) optional and that after the comma I have at least 2 more numbers, the farthest I've come is this
^(([0-9]{0,})+([.]?))+([,]{0,1}?)([0-9]{1,}?)+$
This works fairly well, the problem is that it allows me to put the period (.) Followed more than once for example (100 ... 000), this accepts it, but I need it to only accept one period (.) At a time, how do i fix it?
I need the regex to validate as follows
100 VALID
100.000,00 VALID
100. INVALID
100..00 INVALID
100, INVALID
100..000,00 INVALID
To prevent the same character (in this case a dot) appearing consecutively, use a negative look ahead anchored to start of input:
^(?!.*[.][.])<rest of regex>
In your case:
^(?!.*[.][.])(([0-9]{0,})+([.]?))+([,]{0,1}?)([0-9]{1,}?)+$
See live demo with test cases from question.
I'm not clear on what you actually want to match, but I don't need to understand that to answer your question, which was how to prevent the same character appearing consecutively.

Regex - how to negate a group of substring in a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to create a numeric pattern of 4 digits such that there is at least one non-zero number.
So, it can be "1234", "0001" but not "0000". Also, we must not use the lookahead operator and the lookbehind operator.
my current pattern [0-9]{4}. I can't seem to understand how to remove just "0000".
It's in fact very easy. You will need to specify the following:
four digits, from which the first is not 0
OR
four digits, from which the second is not 0
OR
four digits, from which the third is not 0
OR
four digits, from which the fourth is not 0
Disjunction regex
Use the | operator.
Digit
[0-9]
Non-zero digit
[1-9]
Summary
In this answer you find everything you need. Since this is homework, I will let you work out the formula. Happy thinking!
As per Lajos's suggestion
^[1-9][\d]{3}|^[\d][1-9][\d]{2}|^[\d]{2}[1-9]\d|[\d]{3}[1-9]

Regex for two digit number followed by . for find and replace in vim [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to find out following string pattern in vim and replace it with some thing else. Can you please tell me regex for the same.
1.
11.
20.
21.
99.
basically one / two digits followed by dot.
I think you could do something like the following (I'm not very experienced with VI so there might be a better way)
:%s/\d\+\./MyString/gc
So that's essentially using \d\+\. to search for numbers appearing one or more times followed by a ..
MyString is your replacement string.
:%s is the substitute command, :s would just search the current line.
/gc looks for the match as many times as it appears on the line (g), and asks for confirmation before each replacement (c).
Tried this?
[^0-9][0-9][0-9]\.
Or have you tried it and it didn't work?
It has an issue though of three digits and a dot, i.e. "123." will also be captured
The regex for 1 or 2 digits followed by a dot is:
\<\d\d\?\.
The "word boundary" \< precludes 3 digits (and a dot), which would be allowed without it (the last two digits of a 3-digit number would match).
To replace using this tegex in vi:
s/\<\d\d\?\./foo/g

Regex to match first three octets of IP address [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I see this regular expression will match an IP address:
\b(?:\d{1,3}\.){3}\d{1,3}\b
How do I change it so it will only match the first three octets of an IP address?
So, provided with 1.2.3.4, it will only match 1.2.3.
First of all, the regex you supplied is incorrect. It will match an IP adress, but also something like 192a168.1f12. The reason is because . is a special character in regular expressions which equates to any character. Escape this with a \ so you get \b(?:\d{1,3}\.){3}\d{1,3}\b and you have an IP address regex.
Then, analyze the regex - it consists of two main parts - the part for the a.b.c. part where a, b and c are numbers with one to three digits (which is technically not an IP address since it should go to 255, but it's close enough) and then there's the last one to three digit number part.
Notice that the first part ends with a {3}, specifying it should appear 3 times. You want it to appear only 2 times, followed by the same second part so you get \b(?:\d{1,3}\.){2}\d{1,3}\b.
This should be enough for you, but depending on your requirements, you could also make sure that the first three octets are followed by the last octet (but not match it) by using a positive-lookahead.
Also, you don't really need a non-matching group so you can simplify your regex to this:
\b(\d{1,3}\.){2}\d{1,3}\b
Do this:
address: \b(?:\d{1,3}.){2}\d{1,3}\b
Allright so what does the above mean? well \d matches a digit, the {1,3} means that there should be 1-3 digits no more, no less. the dot . actually matches any character (including dots, more correctly would be \.) Since we have this statement inside of parentheses, and here I have to confess I have no clue which language you're using, although i suspect it's PHP so I don't really know what the ?: does. But the {2} after those says that the pattern will repeat twice, then the last \d{1,3} matches the third octet.

Regex - Without Special Characters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm using regex to validate username
^[a-zA-Z]+\.[a-zA-Z]{4,10}^'
Unfortunately it doesn't affect if the the value contains special characters such as !##$%^&*)(':;
I would glad to get some help for Regex that contains:
Alphanumeric only (a-zA-Z0-9)
Length between 4 - 10 characters.
The conditions you specified do not conform to the regexp you posted.
the regexp you posted ^[a-zA-Z]+\.[a-zA-Z]{4,10}^ is erroneous I guess, because of the ^ in the end, it will never be matched to any expression, if you want to match with the ^ at the end of the expression, you need to escape it like this \^. but ^ alone means "here is the start of the expression", while $ means "here is the end of the expression".
Even though, it denotes:
It starts with alpha (at least 1).
there must be a '.' period character.
Now there must be at least 4 alphas.
The regexp you need is really is:
^[a-zA-Z0-9]{4,10}$
This says:
It starts with alphanumeric.
There can be minimum of 4 and maximum of 10 of alphanumeric.
End of expression.
Try this:
^[a-zA-Z0-9]{4,10}$