1

I have a simple regex which searches a sentence to see if it contains the word of|for|in|at and here is my regex that is almost working in regexpal:

[^A-Za-z]of|for|in|at[^A-Za-z]

I run it on following setences:

show me the weather of seattle
show me the weather in seattle
show me the weather for seattle
show me the weather at seattle

and here are results:

enter image description here

When I use it in my Java code it doesnt works at all. In regexpal too is shows space with for and at which I defined at start and end. Can someone please tell what is wrong with my regex and how to search for one of many words in a sentence

I always get else condition in my java which means it is not matching regex. Here is my java code:

public class Regex 
{
public static void main(String[] args)
{
    String str = "show me the weather in seattle";

    if(str.matches("[^A-Za-z]of|for|in|at[^A-Za-z]")){
        System.out.println("Yayyy!!");
    }
    else{
        System.out.println("OMG now what to do");
    }       
}
}
4
  • 2
    matches checks the string is completely. It is not looking for a match. try add .* at start and end regexp Commented Apr 17, 2012 at 3:22
  • 3
    also, your "space" is only in front of "of" and after "at". use [^A-Za-z](of|for|in|at)[^A-Za-z] or, better, \\W(of|for|in|at)\\W Commented Apr 17, 2012 at 3:24
  • @turbanoff I tried .* but problem is it even matches "show me the weather seattle" when I remove "at" because at is present in weather and seattle. I need independent occurance of any word hence I used [^A-Za-z] Commented Apr 17, 2012 at 3:31
  • @andrewcooke Your code works perfectly in regexcode but not in java. Do I need to append something in str.matches(regex) to make it work for java Commented Apr 17, 2012 at 3:37

2 Answers 2

1

Doesn't this work .*[^A-Za-z](of|for|in|at)[^A-Za-z].* ?. Ofcourse you will need to do extra to check for the boundary cases where of|for|in|at is either the start or the end of the word.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. It works. In my statements those words will appear between the statements only. Regards
You shouldn't need the .* at all. The regular expression is being bound to a non letter before and after any one of the words in the set; adding .* is completely unnecessary and pointless.
@EliSand You didn't understand that the mathes method checks whether the full string is matched by the regex right ? It is like that, so your assumption works only when there is no other "letter" in the string before and after the match. So here it works like - May be some characters, whatever,then a non letter,then what I am looking for,then a non letter,then may be some more characters,whatever :-)
@EliSand And also before saying un-necessary and pointless you should come with a different working answer, do you have any ?
I retract my comment - I assumed that no-one would ever create a function that implicitly implies ^ and $ on a regex. It saves two characters and prevents functionality... alas I didn't write Java. My apologies on the confusion.
1
[^A-Za-z](?:of|for|in|at)[^A-Za-z]

You want to group the "or" matches, otherwise it thinks you mean:

[^A-Za-z]of  OR  for  OR  in  OR  at[^A-Za-z]

which is very likely not what you want.

3 Comments

Pro-tip: the ?: part inside the () makes the match just a group but not usable as a match entity or whatever the technical mumbo jumbo description is.
It works regex pal but not in java. In Java my code again goes to else case, do I need to append something to it to call in str.matches(). And yes I want occurence of 1 of or for or in or at. I want it to be able to see whether user has any of [of|for|in|at] in his statements
If I'm understanding what you're actually trying to achieve with your regular expression, you should use \b(of|for|in|at)\b. Also, does .matches() return true/false or some condition that can be tested as such?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.