-3

I would like to see if a . exists in a string followed by a number

E.g 123.456 = True
E.g 123456. = False
E.g 123456 = False
E.g 123.456. = True

Any Regex genius out there?

2
  • 1
    this is a very basic regex, you do not need a genius, even i can answer. Commented Oct 8, 2012 at 21:15
  • the question is easy if you know the answer Commented Oct 8, 2012 at 21:26

3 Answers 3

5
\.(?=[0-9])

matches a dot iff it's followed by a digit. In Java, that is

Pattern regex = Pattern.compile("\\.(?=[0-9])");
Matcher regexMatcher = regex.matcher(subjectString);
foundMatch = regexMatcher.find();
Sign up to request clarification or add additional context in comments.

4 Comments

How does this compare to \.\d?
Your regex also matches the digit, mine only matches the dot (if it's followed by a digit). The OP's question is a bit vague on whether he wants the digit to be part of the match. It probably doesn't matter...
sorry, but still cannot understand. could you please explain what does that '?=' mean?
thanks a million. im very new to java . To clarify solution i done the following which works. import java.util.regex.Matcher; import java.util.regex.Pattern; Pattern regex = Pattern.compile("\\.(?=[0-9])"); Matcher regexMatcher = regex.matcher(currentNumber); boolean foundMatch = regexMatcher.find(); if (foundMatch) { // do nothing as decimal point already exists } else { decimalPointActive = true; }
2

Regex would be '\\.\\d' I believe

1 Comment

Don't forget to escape the backslashes.
1

This works fine:

String regex="\\.\\d";

Comments

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.