3

My goal is to match the first 0 and everything after that zero in a decimal value. If the first decimal place is a zero then I want to match the decimal too. If there is no decimal then capture nothing.

Here are some examples of what I want:

180.570123 // should capture the "0123" on the end
180.570    // should capture the "0" on the end
180.0123   // should capture the ".0123" on the end
180.0      // should capture the ".0" on the end
180123     // should capture nothing
180        // should capture nothing

If the first decimal place is a 0 then making the match is easy:

(\.0.*)

My problem is matching when the first decimal place is not a 0. I believe positive lookbehind will fix this issue, but I am not able to get it to work correctly. Here is one regex I have tried:

(?<=^.*\..*)0.*

This regex will eventually be used in Java.

UPDATE:

I am going to use this regex to get rid of numbers and possibly a decimal point on the end of a string using Java's replaceAll method. I will do this by replacing the capture group with an empty string. Here is a better example of what I want.

String case1 = "180.570123";
String case2 = "180.570";
String case3 = "180.0123";
String case4 = "180.0";
String case5 = "180123";
String case6 = "180";

String result = null;

result = case1.replaceAll( "THE REGEX I NEED", "" );
System.out.println( result ); // should print 180.57

result = case2.replaceAll( "THE REGEX I NEED", "" );
System.out.println( result ); // should print 180.57

result = case3.replaceAll( "THE REGEX I NEED", "" );
System.out.println( result ); // should print 180

result = case4.replaceAll( "THE REGEX I NEED", "" );
System.out.println( result ); // should print 180

result = case5.replaceAll( "THE REGEX I NEED", "" );
System.out.println( result ); // should print 180123

result = case6.replaceAll( "THE REGEX I NEED", "" );
System.out.println( result ); // should print 180

Also, I am testing these regexs at http://gskinner.com/RegExr/

0

4 Answers 4

3

You can use this expression:

\.[1-9]*(0\d*)

And what you want will be in the first capturing group. (Except the decimal point.)

If you want to capture the decimal point too, you can use:

(?:\.[1-9]+|(?=\.))(\.?0\d*)

Example (online):

Pattern p = Pattern.compile("(?:\\.[1-9]+|(?=\\.))(\\.?0\\d*)");

String[] strs = {"180.570123", "180.570", "180.0123", "180.0", "180123", "180", "180.2030405"};

for (String s : strs) {
    Matcher m = p.matcher(s);
    System.out.printf("%-12s: Match: %s%n", s,
        m.find() ? m.group(1) : "n/a");
}

Output:

180.570123  : Match: 0123
180.570     : Match: 0
180.0123    : Match: .0123
180.0       : Match: .0
180123      : Match: n/a
180         : Match: n/a
180.2030405 : Match: 030405
3
  • That captures case 1 incorrectly and does not capture case 2 or 4 at all. Mar 2, 2012 at 14:10
  • 1
    @typoknig, updated. And case 1 was always matched correctly. Use the content of the first capturing group, not the whole match.
    – Qtax
    Mar 2, 2012 at 14:14
  • Ah, I see. It does seem to be working when use the content of the first capture group as you said. Thanks a lot! Mar 2, 2012 at 14:54
1

I would write a small function to do the extracting instead of regex.

private String getZeroPart(final String s) {
        final String[] strs = s.split("\\.");
        if (strs.length != 2 || strs[1].indexOf("0") < 0) {
            return null;
        } else {
            return strs[1].startsWith("0") ? "." + strs[1] : strs[1].substring(strs[1].indexOf("0"));
        }
    }

to test it:

final String[] ss = { "180.570123", "180.570", "180.0123", 
"180.0", "180123", "180", "180.2030405","180.5555" };

        for (final String s : ss) {
            System.out.println(getZeroPart(s));
        }

output:

0123
0
.0123
.0
null
null
030405
null

update

based on the EDIT of the question. do some changes on the method to get the right number:

private String cutZeroPart(final String s) {
    final String[] strs = s.split("\\.");
    if (strs.length != 2 || strs[1].indexOf("0") < 0) {
        return s;
    } else {
        return strs[1].startsWith("0") ? strs[0] : s.substring(0, strs[0].length() + strs[1].indexOf("0") + 1);
    }
}

output:

180.570123 -> 180.57
180.570 -> 180.57
180.0123 -> 180
180.0 -> 180
180123 -> 180123
180 -> 180
180.2030405 -> 180.2
180.5555 -> 180.5555
1
  • Thanks for this. It is nice to have options, but I think the regex way is a bit cleaner :) Mar 2, 2012 at 16:54
0

Lookbehinds might be overkill. This one worked well-->

/\.\d*(0\d*)/

3
  • That captures case 1 incorrectly and does not capture case 2, 3, or 4 at all. Mar 2, 2012 at 14:15
  • But not that case to capture the decimal and the following 0. What do you expect here: 180.0407
    – Ethan
    Mar 2, 2012 at 14:17
  • The group captures what you want, except for the .0 case.
    – Ethan
    Mar 2, 2012 at 14:18
0

You can try this :

        Matcher m = Pattern.compile("(?<=(\\.))(\\d*?)(0.*)").matcher("180.2030405");
        String res="";
        if(m.find()){
            if(m.group(2).equals("")){
                res = "."+m.group(3);
            }
            else{
                res = m.group(3);
            }
        }

Your Answer

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

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