-1
String haystack = "hey (( howdy";
haystack.matches(".*\\b\\Q((\\E\\b.*");

Line 2 should return true, but it returns false. Is this bug in Java, or am I doing it wrong?

Edit : What I am trying to achieve is see if an user input (complete word) is present in the haystack

q126y
  • 1,589
  • 4
  • 18
  • 50
  • 2
    unescaped `(`... – Hitmands Sep 10 '17 at 15:21
  • 1
    ^ this is your problem (previous comment)... – Jorge Campos Sep 10 '17 at 15:21
  • 2
    @Hitmands Since it is wrapped by `\Q` `\E`, it doesn't require escaping. – q126y Sep 10 '17 at 15:23
  • @JorgeCampos Since it is wrapped by `\Q` `\E`, it doesn't require escaping. – q126y Sep 10 '17 at 15:23
  • Never used \Q or \E before on java, are you sure that Java supports it? Tried without it? – Jorge Campos Sep 10 '17 at 15:25
  • @JorgeCampos https://stackoverflow.com/q/15409296/5455629 If we try without \Q \E it throws exception about "unmatched parenthesis" Also please see my edit of question. Is there another way to achieve my goal? – q126y Sep 10 '17 at 15:27
  • 1
    It works fine for me here: `String test = "some text (( other text"; System.out.println(test.matches(".*\\(\\(.*"));` yields to true. – Jorge Campos Sep 10 '17 at 15:31
  • 4
    Probably because `((` is not a word. Try using space boundaries instead. – shmosel Sep 10 '17 at 15:36
  • [*Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.* - Jamie Zawinski](http://regex.info/blog/2006-09-15/247) –  Sep 10 '17 at 15:51
  • learn to use tools like [RegEx 101](http://regex101.com) before coming here. –  Sep 10 '17 at 15:56

2 Answers2

2

To do what you want you just need to search for the user input.

public static void main(String[] args) {
    String test = "some text (( other text inside a stack";
    String userInput = "((";

    Pattern p = Pattern.compile(".*" + Pattern.quote(userInput) + ".*");
    Matcher m = p.matcher(test);
    System.out.println(m.find());
}

The problem is that (( is not a word, therefore it can't be matched when preceded and suffixed by \b

It prints:

true

Note: To fit in a program that can matches both. You will probably test first if the user input is a word if yes you use boundaries if not this above solution.

Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
  • if user input is `((` It will throw exception – q126y Sep 10 '17 at 15:33
  • I think using space boundary as suggested by @shmosel along with start and end boundary would be easier `.matches(".*(^|\\s)" + Pattern.quote(keyword)+ "(\\s|$).*")` – q126y Sep 10 '17 at 15:48
  • 1
    Have you tested this, it wont work here. That's because `^` can not have anything before it. Also `$` can not have anything after it to work – Jorge Campos Sep 10 '17 at 15:50
  • 1
    @q126y - you have some very basic misconceptions about how regex works given your misuse of fundamental things like `^` and `$`. –  Sep 10 '17 at 15:53
  • @JorgeCampos is my sample program valid, or did I misunderstood what you were trying to say? – q126y Sep 10 '17 at 16:04
  • @JorgeCampos The user input is not at the start and it works https://ideone.com/m04z2K – q126y Sep 11 '17 at 02:26
  • Well it do works, though it is very strange. It shouldn't. It seems that in a regex when the mask `^` preceded by an `.*` can be valid if there is nothing to match that `.*`. That is new to me. – Jorge Campos Sep 11 '17 at 04:24
  • @JarrodRoberson `you have some very basic misconceptions about how regex works given your misuse of fundamental things like ^ and $` what? how? – q126y Sep 11 '17 at 05:01
0

Most likely it has something to do with the '\b' Word Boundary matching as it is implementation dependant according to this article: http://www.regular-expressions.info/wordboundaries.html

Have you tried:

String haystack = "hey (( howdy";
haystack.matches(".*\\b \\Q((\\E \\b.*"));

Note the spaces before/after \b.