-2

I have been facing an issue with regular expression string matching. Here is my string - var String detectionString = Stack Overflow (hello)

I tried this regular expression test - \s(detectionString)\b but could succeed to match a string Stack Overflow only. Can anyone help to provide a regular expression string that can match & include words inside parenthesis also.

Here I tried to match my string and successful but it requires regex expression in between string(square bracket covering parenthesis can solve this problem) like (Stack Overflow [ ( ] hello [ ) ]) . and my problem is I can't add expression in between string.

Note: There can be any kind of string with parenthesis like - var String detectionString = Krunal Bhavsar 123 (iOS).

![enter image description here

Krunal
  • 77,632
  • 48
  • 245
  • 261
  • 2
    Can you include sample input and output? What should show up and what shouldn't? What should pass and fail? – Kevin Johnson Mar 21 '17 at 17:28
  • 3
    Something like `"\\s\\(\(detectionString)\\)"` – Wiktor Stribiżew Mar 21 '17 at 17:29
  • 3
    Why is this tagged with Java (a language) and iOS (an OS)? – rmaddy Mar 21 '17 at 17:32
  • you can try [A-z \(\)]+ . you can make use of this site to try out regex http://regexr.com/ – Vinod Krishnan Mar 21 '17 at 17:32
  • 1
    It's unclear what you're asking. Do you want a regular expression that matches the *specific string* "Stack Overflow (hello)", or are you looking for a means to dynamically construct regular expressions that match *arbitrary* strings, including those containing regex metacharacters? – John Bollinger Mar 21 '17 at 17:36
  • this? (Stack\s*Overflow\s*\([^)]*\)) – Kevin Johnson Mar 21 '17 at 17:38
  • @rmaddy, regular expression has common concept for matching string (in any language. I'm facing problem with iOS (front-end platform) & Java (back-end platform) are supporting platforms for my project, so solution on any platform can solve my problem. – Krunal Mar 22 '17 at 09:08
  • @KevinJohnson - I've to match full string, I can't add any expression in between string. Please suggest me any other solution that I can match full string "Stack Overflow (hello)" using regular expression – Krunal Mar 22 '17 at 09:15
  • 1
    @KrunalBhavsar Whenyou say "can't add any expression in between string" do you mean arbitrary characters inside the parenthesis such as "Stack Overflow (expressionhere)" OR "Stack Overflow expressionhere (hello)" OR ONLY "Stack Overflow (hello)" – Kevin Johnson Mar 22 '17 at 18:08
  • Yes, there can be any string like var testString = "Kevin Johnson (Java Specialist)"... – Krunal Mar 23 '17 at 08:38

2 Answers2

3

I would suggest quoting the whole target string by enclosing it between \Q and \E:

Pattern p = Pattern.compile("\\b\\QStack Overflow (hello)\\E\\b");

That also works very well If you are trying to build patterns dynamically, so that other target strings can be matched. The only caveat in that case is that you need to watch out for the case that the target string contains the two-character substring \E. If it does, then you'll have escape it.

For example, you might insert \E\Q between the literal \ and E. That is, to match the string This string has literal \E inside you could use

Pattern p = Pattern.compile(
        "\\b\\QThis string has literal \\\\E\\QE inside\\E\\b");

Note also the distinction between escape syntax for String literals and escape sequences for interpretation by the regex engine. These are separate layers.

Update:

Additionally, I have more or less followed your original pattern attempt with respect to the match boundaries -- that is, by leading and trailing with \b to match word boundaries. It turns out, however, that the end of the input does not constitute a word boundary for Java's purposes (though the beginning does). Therefore, it may better suit your purpose to use a zero-width assertion in one or both places. For example:

Pattern p = Pattern.compile("(?<!\\w)\\QStack Overflow (hello)\\E(?!\\w)");

Naturally, you may need to tweak the details to your specific needs, but this does work in my tests.

![enter image description here

Krunal
  • 77,632
  • 48
  • 245
  • 261
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • I tried your suggestion on https://regex101.com/ - but not getting successful result. Please provide me any other solution, if you've. – Krunal Mar 22 '17 at 09:17
  • @KrunalBhavsar, regex101 does not support Java's flavor of regex. It does support PCRE, which should be close, but you are better off testing with true Java regexes, which you can do at [RegexPlanet](http://www.regexplanet.com/advanced/java/index.html). Playing around a bit there, I find that the regexen I have suggested work fine, with the caveat that the trailing `\b` does not match at the very end of the string. I will momentarily amend my answer to address that. – John Bollinger Mar 22 '17 at 13:56
0

Something like:

(Stack Overflow)\s(\(hello\))
aaroncio
  • 307
  • 1
  • 6
  • I updated my question. I can't add expression in between string. Please suggest me any other solution. – Krunal Mar 22 '17 at 09:23