help with regex

R

ruds

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.
 
L

Lasse Reichstein Nielsen

ruds 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]*");

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
 
R

Roedy Green

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
 
P

Patricia Shanahan

ruds said:
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
 
R

ruds

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.
 
R

Roedy Green

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
 
J

Jussi Piitulainen

ruds said:
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top