help with regex

Discussion in 'Java' started by ruds, Jun 8, 2008.

  1. ruds

    ruds Guest

    hi,
    I'm using java.util.regex for finding exponential numbers from a file.
    the expression I'm using is:
    Pattern p2 = Pattern.compile("[0-9]*\\.[0-9]*[E]\\+[0-9]*");

    I'm not able to get the result.Please tell where I'm going wrong.
     
    ruds, Jun 8, 2008
    #1
    1. Advertising

  2. ruds <> writes:

    > I'm using java.util.regex for finding exponential numbers from a file.
    > the expression I'm using is:
    > Pattern p2 = Pattern.compile("[0-9]*\\.[0-9]*[E]\\+[0-9]*");


    This matches a sequence of:
    zero or more digits
    a decimal point
    zero or more digits
    an "E" (capital)
    a "+"
    zero or more digits.

    I.e., it matches ".E+" as well as "0.0E+0", or any amount of digits
    where the zeros are.
    It cannot omit any of "." or "E+", so it doesn't match "0", "0.0",
    "1E+4" or similar traditional decimal value representations.

    > I'm not able to get the result.


    Which result. You have not explained what the exact result you want
    is, nor how it fails.
    I.e., which texts does it accept that it shouldn't, or which texts
    does it fail to accept that it should?

    > Please tell where I'm going wrong.


    When working with regular expressions, always first describe what
    strings you want to match, in plain language. Or use syntax diagrams
    or BNF grammars or another formal notation, but formalize what you
    want, because regular expressions are a pain to write, and when they
    get complex, they are pretty much impossible to read.

    I'm *guessing* that you want to match all valid Java floating point
    literals in exponential form.
    I.e. a sequence of
    zero or more digits \
    an optional decimal point |- i.e. one or more digits with an optional
    one or more digits / decimal point somewhere before the end
    an "e" or "E"
    an optional "+" or "-"
    one or more digits.

    That regexp could be
    "\\d*\\.?\\d+[eE][\\-+]?\\d+"
    (In regexps, \d is the same as [0-9])

    Try it out, and if it doesn't give what you want, tell us what that is :)

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jun 8, 2008
    #2
    1. Advertising

  3. ruds

    Roedy Green Guest

    On Sun, 8 Jun 2008 04:29:50 -0700 (PDT), ruds <>
    wrote, quoted or indirectly quoted someone who said :

    >Pattern p2 = Pattern.compile("[0-9]*\\.[0-9]*[E]\\+[0-9]*");
    >
    >I'm not able to get the result.Please tell where I'm going wrong.


    some thoughts .

    E does not match e.

    + won't match -.

    pattern won't match 1 1.0 1e6
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jun 8, 2008
    #3
  4. ruds wrote:
    > hi,
    > I'm using java.util.regex for finding exponential numbers from a file.
    > the expression I'm using is:
    > Pattern p2 = Pattern.compile("[0-9]*\\.[0-9]*[E]\\+[0-9]*");
    >
    > I'm not able to get the result.Please tell where I'm going wrong.


    The API documentation for Double.valueOf(String) includes sample code
    for recognizing its valid inputs using a regular expression. I suggest
    starting from that code, and deleting the pieces you don't want for your
    case.

    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Double.html#valueOf(java.lang.String)

    Patricia
     
    Patricia Shanahan, Jun 8, 2008
    #4
  5. ruds

    ruds Guest

    I want to match exponential nos. viz: 1.87082E+11,2.87082E+11,etc. and
    extract those lines containing them.
    but, I not able to get correct match with the above mentioned regex.
     
    ruds, Jun 9, 2008
    #5
  6. ruds

    Roedy Green Guest

    On Sun, 8 Jun 2008 04:29:50 -0700 (PDT), ruds <>
    wrote, quoted or indirectly quoted someone who said :

    >I'm using java.util.regex for finding exponential numbers from a file.
    >the expression I'm using is:
    >Pattern p2 = Pattern.compile("[0-9]*\\.[0-9]*[E]\\+[0-9]*");
    >
    >I'm not able to get the result.Please tell where I'm going wrong.


    I suspect your main problem is not the pattern but the code that uses
    is.

    Make sure you understand the different between matching and finding
    see http://mindprod.com/jgloss/regex.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jun 9, 2008
    #6
  7. ruds writes:

    > I want to match exponential nos. viz: 1.87082E+11,2.87082E+11,etc. and
    > extract those lines containing them.
    > but, I not able to get correct match with the above mentioned regex.


    You did not write, compile and run a small self-contained program that
    shows even one example of the failure of your expectations. That might
    solve your problem already. If not, posting it here might solve your
    communication problem.

    Look here:

    import java.util.regex.Pattern; import java.util.regex.Matcher;
    class M {
    public static void main(String [] args) {
    Pattern p2 = Pattern.compile("[0-9]*\\.[0-9]*[E]\\+[0-9]*");
    String line = "nos. viz: 1.87082E+11,2.87082E+11,etc. and";
    System.out.println(p2.matcher(line).matches()); // false
    System.out.println(p2.matcher(line).lookingAt()); // false
    System.out.println(p2.matcher(line).find()); // true
    }
    }

    This is already longer than you need. I compiled it. I ran it. I even
    added the three comments, so you can now simply tell which of them, if
    any, you find unexpected.

    (I know it contains a redundant import. I leave it in, because it may
    not stay redundant when you experiment with modifications.)
     
    Jussi Piitulainen, Jun 9, 2008
    #7
    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. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    712
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,634
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    600
  4. Xah Lee
    Replies:
    1
    Views:
    943
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    769
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page