1

I have following string 3.14, 123.56f, .123e5f, 123D, 1234, 343E12, 32. What I want to do is match any combination of above inputs. So far I started with the following:

^[0-9]\d*(\.\d+)

I realize I have to escape the . since its a regular expression itself.

Thanks.

5
  • 1
    What languag eare you using the regex from?
    – xanatos
    Oct 10, 2011 at 17:34
  • Do you know why use sometimes use [0-9] and sometimes \d?
    – Mark Byers
    Oct 10, 2011 at 17:34
  • It isn't clear the D what is and what modifiers it's compatible with
    – xanatos
    Oct 10, 2011 at 17:37
  • Can you clarify what you mean by above inputs? Oct 10, 2011 at 17:40
  • @xanatos I was actually trying to figure a generic regex but mainly this for java and in the sense of floating point numeric constants. Oct 10, 2011 at 17:40

5 Answers 5

2

This should also work, if not already proposed.

try {
    Pattern regex = Pattern.compile("\\.?\\b[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?[fD]?\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    Matcher regexMatcher = regex.matcher(subjectString);
    while (regexMatcher.find()) {
        // matched text: regexMatcher.group()
        // match start: regexMatcher.start()
        // match end: regexMatcher.end()
    } 
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}
2

Probably

^(\d+(\.\d+)?|\.\d+)([eE]\d+)?[fD]?$

http://regexr.com?2ut9t

^ start of the string
(\d+(\.\d+)?|\.\d+) one or more digits with an optional ( . and one or more digits)
  or
. and one or more digits
([eE]\d+)? an optional ( e or E and one or more digits)
[fD]? an optional f or D
$ end of the string

As a sidenote, I've made the D compatible with everything but the f.

If you need positive and negative sign, add [+-]? after the ^

2
  • Yes, but you used non capturing groups, and your regex will succeed for empty string (you have everything optional)
    – xanatos
    Oct 10, 2011 at 17:44
  • Everyone that uses Regexes loses before or later! :-) :-) This is the only hard truth.
    – xanatos
    Oct 10, 2011 at 17:52
2

This will match all of those:

[0-9.]+(?:[Ee][0-9.]*)?[DdFf]?

Note that within a character class (square brackets), dot . is not a special character and should not be escaped.

1

Maybe that one ?

^\d*(?:\.\d+)?(?:[eE]\d+)?(?:[fD])?$

with

^\d*         #possibly a digit or sequence of digits at the start
(?:\.\d+)?    #possibly followed by a dot and at least one digit
(?:[eE]\d+)?  #possibly a 'e' or 'E' followed by at least one digit
(?:[fD])?$    #optionnaly followed by 'f' or 'D' letters until the end
1
  • Caution: as xanatos pointed above: this will also holds true for empty strings.
    – Laurent'
    Oct 10, 2011 at 17:52
0

You can use regexpal to test it out, but this seems to work on all of those examples:

^\d*\.?(\d*[eE]?\d*)[fD]?$

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.