Regular Expression to retrieve length

133 views Asked by At

I want to retrieval number of length, such as 12m, 12 m, 3,2 meters but don't get the value of areas 3m2 or 3m3 .... I use the regular expression to get value. This is my regular expression but it's not run. :(

([0-9]+[,.]*[0-9]*\s*(mét|m|cm|dm|mm|km|hm|dam|centimet)+(^[0-9]+))*
3

There are 3 answers

0
Spotted On

Other approach (every test passing):

import static org.junit.Assert.*;

import org.junit.Test;

public class RemoveUnitSpec {
    public static String removeUnit(String input) {     
        for (int i = 0; i < input.length(); i++) {
            if(Character.isLetter(input.charAt(i))) {
                return input.substring(0, i).trim();
            }
        }
        return input;
    }

    @Test
    public void itShouldDoNothingWhenNoUnit() {
        String input = "3,52";

        assertEquals("3,52", removeUnit(input));
    }

    @Test
    public void itShouldRemoveUnit() {
        String input = "12m";

        assertEquals("12", removeUnit(input));
    }

    @Test
    public void itShouldRemoveSquareMeter() {
        String input = "12m2";

        assertEquals("12", removeUnit(input));
    }

    @Test
    public void itShouldRemoveUnitWithSpace() {
        String input = "12 m";

        assertEquals("12", removeUnit(input));
    }

    @Test
    public void itShouldRemoveTextualUnitWithSpace() {
        String input = "3,2 meters";

        assertEquals("3,2", removeUnit(input));
    }
}
0
Maurice Perry On

I think I would use something like so:

([0-9]+(?:[,.][0-9]*)?)\s*(mét|m|cm|dm|mm|km|hm|dam|centimet)([0-9]+)?

You can then obtain each part of the string as follows:

Matcher matcher = RE.matcher(string);
if (matcher.matches) {
    String value = matcher.group(1);
    String unit = matcher.group(2);
    String exp = matcher.group(3); // may be null
    ...
}
0
Gene On

Try

[0-9]+([,.][0-9]*)?\s*(mét|m|cm|dm|mm|km|hm|dam|centimet)\s*([^\s0-9]|\z)

Explanation:

  • Match 1 or more digits
  • Optionally match a comma or dot followed by zero or more digits.
  • Match zero or more spaces.
  • Match a unit string.
  • Match zero or more spaces
  • Match either a character that's not a digit or space or end of input.