Sign in to follow this  

Need some help with this regular expression

This topic is 4516 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've recently come across regular expressions, which has helped me a great deal with my parser for my graphing calculator command line. I'm trying to write an expression to extract numbers. As I see it, numbers can be like: 34.5656 232 .24234 I have this: [\d]*[\.]??[\d]* as my regular expression, and it follows the Java RegEx rules. As I interpret it: get zero or more numerical digits, get either one or zero .(the dot, for a decimal), get zero or more numerical digits unfortunately, this doesn't seem to be working, and since I'm with a java ide that doesn't have a debugger, I'm at a loss. Thanks in advance to anyone who can help me out.

Share this post


Link to post
Share on other sites
Here is where I'm using it:

Main:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class ConsoleCalc
{

public static void main(String[] args)
{
//Expression exp = new Expression("9999.0");

String test = "aa9945.8sdfa";

Pattern p = Pattern.compile("[\\d]*[\\.]??[\\d]*");
Matcher m = p.matcher(test);

if(m.find())
System.out.println(test.substring(m.start(),m.end()));
}
}




Expression:


import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.*;

class Expression
{
public Pattern p;
public Matcher m;

String val;

public Expression(String e)
{
val = e;

List temp = new LinkedList();

System.out.println("Starting Tokenizer");

// Tokenize it
while(e.length() != 0)
{
if(e.charAt(0) == ' ') // WhiteSpace
{
e = e.substring(1,e.length());
}
else if(e.charAt(0) == '+') // Addition
{
temp.add(new Atom("+", Atom.ADD));
e = e.substring(1,e.length());
}
else if(e.charAt(0) == '-') // Substraction
{
temp.add(new Atom("-", Atom.SUBSTRACT));
e = e.substring(1,e.length());
}
else if(e.charAt(0) == '*') // Multiplication
{
temp.add(new Atom("*", Atom.MULTIPLY));
e = e.substring(1,e.length());
}
else if(e.charAt(0) == '/') // Division
{
temp.add(new Atom("/", Atom.DIVIDE));
e = e.substring(1,e.length());
}
else if(e.charAt(0) == '^') // Exponents
{
temp.add(new Atom("^", Atom.EXPONENT));
e = e.substring(1,e.length());
}
else if(e.charAt(0) == '=') // Exponents
{
temp.add(new Atom("=", Atom.EQUALS));
e = e.substring(1,e.length());
}
else // Numbers, Functions, and Variables ... Oh MY!!!111
{
System.out.println("Got to NFV");

// Check for numbers
p = Pattern.compile("[\\d]*[\\.]??[\\d]*");
m = p.matcher(e);

if(m.find() && m.start() == 0 && m.end() != 0)
{
System.out.println("Checking for number " + m.start() + " " + m.end());
temp.add(new Atom(e.substring(m.start(),m.end()), Atom.NUMBER));
System.out.println(e.length());
e = e.substring(m.end());
continue;
}


System.out.println("Checking for Function");

// Check for functions
p = Pattern.compile("[a-zA-Z]{1}[/w]*\\)");
m = p.matcher(e);

if(m.find() && m.start() == 0 && e.charAt(m.end()) == '(')
{
// Start looking for the end
// '(' = +1, ')' = -1
int theEnd = -1;

for(int i = m.start() + 1, balance = 1; i < e.length(); i++)
{
if(e.charAt(i) == '(')
balance++;
else if(e.charAt(i) == ')')
balance--;

if(balance == 0)
{
theEnd = i;
break;
}
}

if(theEnd == -1)
throw new NullPointerException("Not actually a null pointer -> There was an imbalance in parenthesis.");

temp.add(new Atom(e.substring(m.start(),theEnd), Atom.VARIABLE));
e = e.substring(theEnd);
continue;
}




}
}

System.out.println(temp.size());

while(temp.size() != 0)
{
System.out.println(temp.get(0));
temp.remove(0);
}

System.out.println("Exited the tokenizer, number of atoms is: " + temp.size());
}
}





and Atom



class Atom
{
public static final int NUMBER = 0;
public static final int ADD = 1;
public static final int SUBSTRACT = 2;
public static final int MULTIPLY = 3;
public static final int DIVIDE = 4;
public static final int EXPONENT = 5;
public static final int MODULUS = 6;
public static final int FUNCTION = 7;
public static final int VARIABLE = 8;
public static final int EQUALS = 9;

String val;
int type;
Double numericalValue;

public Atom(String Value, int Type)
{
val = Value;
type = Type;

if(type == NUMBER)
numericalValue = new Double(Value);
}

public String toString()
{
return "Atom: " + "String Val = " + val + " Type = " + type;
}
}




[Edited by - Sagar_Indurkhya on August 3, 2005 1:46:41 PM]

Share this post


Link to post
Share on other sites

This topic is 4516 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this