Regular Expression

Discussion in 'Java' started by the87boy@gmail.com, Nov 9, 2006.

  1. Guest

    Why do this regular expression not work:
    if (str.matches("^\\d*$") || str.matches("^\\d*" + dot + "$") ||
    str.matches("^\\d*" + dot + "\\d*$")) {
    return true;
    } else {
    return false;
    }

    dot is a string, who looks like this:
    String dot = ",";

    The 1st regular expression test if it is just a number
    The 2nd regular expression test if it is a number and ends with a dot
    The 3rd regular exression test if it is a dot sorrunded by numbers
    It's only number (can be with a dot)

    Can somebody tell me, what's wrong and maybe shorten it?
    , Nov 9, 2006
    #1
    1. Advertising

  2. writes:

    > Why do this regular expression not work:
    > if (str.matches("^\\d*$") || str.matches("^\\d*" + dot + "$") ||
    > str.matches("^\\d*" + dot + "\\d*$")) {
    > return true;
    > } else {
    > return false;
    > }
    >
    > dot is a string, who looks like this:
    > String dot = ",";
    >
    > The 1st regular expression test if it is just a number
    > The 2nd regular expression test if it is a number and ends with a dot
    > The 3rd regular exression test if it is a dot sorrunded by numbers
    > It's only number (can be with a dot)
    >
    > Can somebody tell me, what's wrong and maybe shorten it?


    It appears to work. It matches ",14" and "3," and "," in addition to
    "314" and "3,14", though. You failed to give an example of a str that
    you think should match and does not, or that you think should not
    match and does.

    "foo|bar|baz" matches "foo" or "bar" or "baz".
    "foo,?bar" matches "foobar" or "foo,bar".

    The anchors are redundant with .matches().
    Jussi Piitulainen, Nov 9, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith-Earl
    Replies:
    1
    Views:
    450
    Mary Chipman
    Jun 15, 2004
  2. VSK
    Replies:
    2
    Views:
    2,287
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    844
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    11,948
    vforvikash
    Dec 29, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    232
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page