Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > help with regex

Reply
Thread Tools

help with regex

 
 
ruds
Guest
Posts: n/a
 
      06-08-2008
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.
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      06-08-2008
ruds <(E-Mail Removed)> 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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-08-2008
On Sun, 8 Jun 2008 04:29:50 -0700 (PDT), ruds <(E-Mail Removed)>
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
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      06-08-2008
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/...va.lang.String)

Patricia
 
Reply With Quote
 
ruds
Guest
Posts: n/a
 
      06-09-2008
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-09-2008
On Sun, 8 Jun 2008 04:29:50 -0700 (PDT), ruds <(E-Mail Removed)>
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
 
Reply With Quote
 
Jussi Piitulainen
Guest
Posts: n/a
 
      06-09-2008
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.)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments