-1

I am trying to match a keyword with following string

"abc,pqr(1),xyz"

It will be succesfull match if the whole one word matched for e.g. "par" or "abc" or "xyz"

Can anyone please help me in creating regex for this match ?

String text    = "hello, hellos(1),bye";
    String keyword = "account";
    String patternString = "["+ keyword + "]";

    Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);

    Matcher matcher = pattern.matcher(text);

    boolean matches = matcher.matches();

    System.out.println("matches = " + matches);
Happy
  • 169
  • 1
  • 4
  • 10
  • 2
    there are some very usefull resources on the net. Like [regex testers](https://regex101.com/) that give you explanation as you write your regex. Pretty nice when you want to learn how to use it. – jhamon Feb 01 '17 at 15:29
  • 1
    You could try replacing `"[" + keyword + "]"` with `"\\b" + keyword + "\\b"` and that should work. –  Feb 01 '17 at 17:06
  • @Andrevin is right, but be aware that: A) it will also find words inside the parenthesis B) if compound words are allowed between commas then either part can match C) if there are hyphens in words, those will also be treated as word barriers (`\b`). – Patrick Parker Feb 01 '17 at 17:40
  • Possible duplicate of [Regex to match multiple strings](http://stackoverflow.com/questions/698596/regex-to-match-multiple-strings) – J E Carter II Feb 01 '17 at 18:59

1 Answers1

0

This Should Work.

([a-zA-Z]+)

Input:

"abc,pqr(1),xyz"

Output:

abc
pqr
xyz

See: https://regex101.com/r/Us6G3X/2

  • Thanks, but it will match if my keyword has alphabets or not what I want is : my keyword = hello match string = "hello, hellos(0),bye" it should only match to hello not to hellos also not to hell – Happy Feb 01 '17 at 15:41