Results 1 to 6 of 6

Thread: Regex - compose a string

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Regex - compose a string

    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.

  2. #2
    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.

  3. #3
    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.

  4. #4
    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.

  5. #5
    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;
    	}

  6. #6
    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.

Similar Threads

  1. Problem in retrieving string with Regex
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 05:53 AM
  2. Regex string with quotes
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 07-02-2010, 06:03 AM
  3. Problem with Regex in returning string
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 03:37 AM
  4. Regex replace an integer with string
    By New ID in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 01:35 AM
  5. Regex strip out all non alpha Characters from a String
    By Chhaya in forum Software Development
    Replies: 2
    Last Post: 22-05-2009, 06:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,038,791.47477 seconds with 17 queries