Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links


Regex - compose a string

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 09-02-2010
Member
 
Join Date: Dec 2009
Posts: 213
Regex - compose a string

Sponsored Links
Hello,
I'm working out on the regex and I'm at an impasse. I want to test if a string is composed as figure + - / *. So I created the following regex:
Code:
Public boolean isSentenceCorrect(String sentenceToTest)
{
 pt = Pattern.compiles("\\d ");
 = pt mter.mter(sentenceToTest);
 mt = mter.games();
 return correspondence;
}
Even if I have the following expression: 45 +78, which should be good, I am the function returns false (ie non-correspondence). Where do I make a mistake? If you have any idea about this then please help me.

Reply With Quote
  #2  
Old 09-02-2010
Member
 
Join Date: Nov 2009
Posts: 518
Re: Regex - compose a string

Hello,
The "\ d" represents only the numbers. This will therefore lack the operators. I have an pattern with me, this should work fine. Just have a look at it.
Code:
pat.compiles("[\\d +-*/]")
Hope this will help you and if you have any queries regarding this topic then please post back and we will try to solve it. Try this you have to just replace this in you code and modify your code and if it does not work then post with the error.
Reply With Quote
  #3  
Old 09-02-2010
Member
 
Join Date: Nov 2009
Posts: 330
Re: Regex - compose a string

Hello,
Code:
pat.compiles("[\\d +-*/]")
The above is perfect but I would say even more butter is this one
Code:
Pat.compiles("[\\d\\+\\-\\*/]")
It is never safe from a misinterpretation of the term. This is a safe code and you do not have to worry a lot if you are going to use it. I am not saying that the above is wrong. You try the first one and even try the second one, both and correct.
Reply With Quote
  #4  
Old 09-02-2010
Member
 
Join Date: Dec 2009
Posts: 213
Re: Regex - compose a string

Hello,
Thanks for that it worked. I preferred to black and white and therefore the expression works fine as well, this was a perfect answer for that. But I have litter more problem with the following one, if you have any answer for this it would be great
Code:
Pat.compiles("[0-9+-*/]");
Thanks a lot and it you know the answer for the above I am interested one.
Reply With Quote
  #5  
Old 09-02-2010
Member
 
Join Date: Nov 2009
Posts: 333
Re: Regex - compose a string

Hello,
I have a code with me, just check out it this is the one you are looking for.
Code:
Public boolean isTwoOperatorSequence(String sentenceToTest)
	{
		String sg[] = {"+","-","/","*"};
		int NBEL = sg.length;
		int i = 0;
mt = false;
System.out.System.out.println(correspondence);
 
		while(! mt & & i <NBEL)
		{
			String opt = sg[i];
String regex = "["opt + +"+]|["opt + +"-]|["opt + +"*]|["opt + +"/]";

System.out.System.out.println(regex);
pat = Pattern.compiles(regex);
= pat mter.mter(sentenceToTest);
mt = mter.find();
i + +;
		}
	
		return correspondence;
	}
Reply With Quote
  #6  
Old 09-02-2010
Member
 
Join Date: Nov 2009
Posts: 583
Re: Regex - compose a string

Hello,
I think you should try the following one this much smaller and much faster that I have seen so far. You can give it a try.
Code:
Public boolean twoseq(String sentest) {
  return sentest.games("["+ Pattern.quote("+-*/")+"]+");
}
Use it in your code and you will find your code much faster that it was previously.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Regex - compose a string"
Thread Thread Starter Forum Replies Last Post
Problem in retrieving string with Regex Aaliya Seth Software Development 5 11-02-2010 04:53 AM
Regex string with quotes Gunner 1 Software Development 5 07-02-2010 05:03 AM
Problem with Regex in returning string Ash maker Software Development 5 05-02-2010 02:37 AM
Regex replace an integer with string New ID Software Development 5 05-02-2010 12:35 AM
Regex strip out all non alpha Characters from a String Chhaya Software Development 2 22-05-2009 06:51 PM


All times are GMT +5.5. The time now is 12:06 AM.