Jump to content
  • Advertisement
Sign in to follow this  
Sagar_Indurkhya

Need some help with this regular expression

This topic is 4736 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
Advertisement
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
Note that regex should always succeed [what if all 3 pieces are 0?]. Might be the source of your difficulties.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!