How to check for specific pattern ???
Hi,
Am very new to java and writing my first codes. I would like to know how to check for a specific pattern in a string value like "ax^2+bx+c".
I would like to know how I can be know if the user typed this correctly, [a, b, c] being integer values.
Thank you all
Re: How to check for specific pattern ???
In java there is the String.split(String regex) function.
In other words you can do
Code:
String[] parts = "a,b,c".split(",");
This will split the the text in parts and will give you an array with values:
a
b
c
The splitter sign "," is more than just a string, it's actually a regular expression pattern.
That means you can also use more complicated patterns to split text, you can make special
split conditions for example: the split sign has to be "," or ";" can be done with syntax like "[,;]".
Now I guess you want to split it based on the mathematical operators (+, -, /, ^, ...)
Let's assume you want to split with a "+". Then you have to be aware that + is a special sign in
the regular expression syntax.
BTW: for more information about the rules of regular expressions:
http://java.sun.com/developer/techni...ases/1.4regex/
Special characters have to be preceeded by a "\" sign.
In java the "\" is also a special character, which should also be proceeded by a "\".
So you'll need "\\" before every special character.
So I think what you need is the following:
Code:
"ax^2+bx+c".split("\\+");
This will split it in the following parts:
ax^2
bx
c
Or perhaps you only want to get a,b and c.
Code:
"ax^2+bx+c".split("[\\+\\^x]");
This will almost do the trick but it will also create empty strings in the array.
To avoid this you should add a + at the end of the the pattern:
Code:
"ax^2+bx+c".split("[\\+\\^x]+");
This plus means that the splitter shouldn't be just 1 character. It can be multiple characters.
Now you'll get an array with elements { "a", "b", "c" }
If you want to validate these I suggest you loop through them and try to parse them to integers
Code:
for (int i=0; i<parts.length; i++)
{
Integer.valueOf(parts[i]);
}
Now this will throw an exception if it fails. So perhaps it's better to wrap it with a try-catch structure
So I think what you want is a function like this.
(I added the "-" and "/" operators as well in this)
Code:
public boolean isValidExpression(String pExpression)
{
String[] parts = pExpression.split("[\\+\\-\\^\\\\x]+");
if ((parts == null) || (parts.length == 0)) return false;
String tmp;
for (int i=0; i<parts.length; i++)
{
tmp = parts[i].trim();
// ignore empty values
// (the first value can be empty if pExpression starts with a "-")
if ("".equals(tmp)) continue;
try
{
Integer.valueOf(tmp);
}
catch(Exception e)
{
return false;
}
}
return true;
}
Re: How to check for specific pattern ???
Quote:
Originally Posted by
WaZda
Hi,
Am very new to java and writing my first codes. I would like to know how to check for a specific pattern in a string value like "ax^2+bx+c".
I would like to know how I can be know if the user typed this correctly, [a, b, c] being integer values.
Thank you all
Hm, did I understand the question correctly ?
You want to know if a b and c are integers, right?
only x can be a variable, right ?
in other words:
"12x^2+4x+7" = true
"12x^2+cx+7" = false
Re: How to check for specific pattern ???
hi Bram Vandenbon,
Thank you so much for your quick reply. Your first post is very helpful. I actually have the mathematical formula "ax^2+bx+c" which you wrote correctly as "12x^2+4x+7". a,b,c being integer values are another control I will implement later, but first i want to be sure the user typed in a CORRECT equation if you see what I mean. So if i should use your split function then it should be "x^2" then the other "x" because a,b,c can be negative value so i can not use the "+/-" as parameter as they will tell me if I have a negative value or not.
Please let me know if am clearer and thank you again for your reply