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!

Recommended Answers

All 2 Replies

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

thanks so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.