3

I need to check that a file contains some amounts that match a specific format:

  • between 1 and 15 characters (numbers or ",")
  • may contains at most one "," separator for decimals
  • must at least have one number before the separator
  • this amount is supposed to be in the middle of a string, bounded by alphabetical characters (but we have to exclude the malformed files).

I currently have this:

\d{1,15}(,\d{1,14})?

But it does not match with the requirement as I might catch up to 30 characters here.
Unfortunately, for some reasons that are too long to explain here, I cannot simply pick a substring or use any other java call. The match has to be in a single, java-compatible, regular expression.

3
  • 1
    It would be useful to know if you know the limits of what's being checked. It is, you have the exact string to match against a ^...$ expression, or you try to match that expression in the middle of other things... Commented Sep 3, 2010 at 10:28
  • I'm matching in the middle of other things, I'll update the question. Commented Sep 3, 2010 at 10:57
  • In that case replace ^ and $ in my answer with \b Commented Sep 3, 2010 at 11:11

1 Answer 1

11
^(?=.{1,15}$)\d+(,\d+)?$
  • ^ start of the string
  • (?=.{1,15}$) positive lookahead to make sure that the total length of string is between 1 and 15
  • \d+ one or more digit(s)
  • (,\d+)? optionally followed by a comma and more digits
  • $ end of the string (not really required as we already checked for it in the lookahead).

You might have to escape backslashes for Java: ^(?=.{1,15}$)\\d+(,\\d+)?$

update: If you're looking for this in the middle of another string, use word boundaries \b instead of string boundaries (^ and $).

\b(?=[\d,]{1,15}\b)\d+(,\d+)?\b

For java:

"\\b(?=[\\d,]{1,15}\\b)\\d+(,\\d+)?\\b"

More readable version:

"\\b(?=[0-9,]{1,15}\\b)[0-9]+(,[0-9]+)?\\b"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply. I updated the requirement with a missing part but was able to adapt your proposal so that it fit in the enclosing regexp. I end up with (?=[,0-9]{1,15}[a-zA-Z])\d+(,\d+)? and it works with the checks that are done previously.
@gizmo \b won't work if the number is immediately followed by a letter. But looks like you've already figured it out.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.