0

Hi, I am quite new to regular expression and I am kinda stuck on this question. I am using a book which explains it only in short detail. I have an answer but am not sure if I got it right. If someone can please check it and post the correct answer. Thanks, heres the question:

Write down regular expressions that define a pattern to recognise a decimal literal. Decimal literals start with a numeral consisting of one or more digits. This may be followed by an optional decimal part consiting of a decimal point '.' followed by another numeral consisting of one or more digits. Finally this may be followed by an optional exponent consisting of the letter 'E' followed by an optional sign followed by a numeral consisting of one or more digits.

Here's my answer:

([0-9]+ "." [0-9]+ (E|.)* [0-9]+)

I really struggled on the end part of the question. If someone can please have an answer so i have something to work on and learn from on. Thanks!

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by sid99
0

Hi below regulary expression should solve your problem . it checks for integers as well as decimal numbers with or without exponent.

^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$

it matches 23 , -17.e23 , +.23e+2. hope this helps

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.