tags:

views:

144

answers:

5

Hi, I have tried 2 questions, could you tell me whether I am right or not?

  1. Regular expression of nonnegative integer constants in C, where numbers beginning with 0 are octal constants and other numbers are decimal constants.

    I tried 0([1-7][0-7]*)?|[1-9][0-9]*, is it right? And what string could I match? Do you think 034567 will match and 000083 match?

  2. What is a regular expression for binary numbers x such that hx + ix = jx?

I tried (0|1){32}|1|(10)).. do you think a string like 10 will match and 11 won’t match?

Please tell me whether I am right or not.

+1  A: 

Your regex for integer constants will not match base-10 numbers longer than two digits and octal numbers longer than three digits (2 if you don't count the leading zero). Since this is a homework, I leave it up to you to figure out what's wrong with it.

Hint: Google for "regular expression repetition quantifiers".

Amarghosh
+1  A: 
  1. 0([1-7][0-7])?|[1-9][0-9] is wrong because there's no repetition - it will only match 1 or 2-character strings. What you need is something like 0[0-7]*|[1-9][0-9]*, though that doesn't take hexadecimal into account (as per spec).
  2. This one is not clear. Could you rephrase that or give some more examples?
Max Shawabkeh
Hi Max, How did u arrive at this answer, could u throw some light?
It's pretty simple. There's the basic | branch between the octal on the left and the decimal on the right. The octal starts with a zero and is then followed by any number of digits between 0 and 7. Decimals are the same as your original regex.
Max Shawabkeh
should there be another [1-7] in front of [0-7]*?
If you want to allow multiple leading zeroes (e.g. 0051) then no.
Max Shawabkeh
+2  A: 

You can always use http://www.spaweditor.com/scripts/regex/ for a quick test on whether a particular regex works as you intend it to. This along with google can help you nail the regex you want.

Learning
A: 

max answered the first question.

the second appears to be the unsolvable diophantine equation of fermat's last theorem. if h,i,j are non-zero integers, x can only be 1 or 2, so you're looking for

^0*10?$

does that help?

jspcal
but jspcal is my answer to second one ((0|1){32}|1|(10)) correct?
it you remove the first term `(0|1){32}|` should work.... (prepend 0* if you want to match strings like "000001", and you would need to anchor the pattern (^$)
jspcal
A: 

There are several tool available to test regular expressions, such as The Regulator.

If you search for "regular expression test" you will find numerous links to online testers.

Hans Kesting