0

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);
4
  • 2
    there are some very usefull resources on the net. Like regex testers that give you explanation as you write your regex. Pretty nice when you want to learn how to use it. Commented Feb 1, 2017 at 15:29
  • 1
    You could try replacing "[" + keyword + "]" with "\\b" + keyword + "\\b" and that should work. Commented Feb 1, 2017 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). Commented Feb 1, 2017 at 17:40
  • Possible duplicate of Regex to match multiple strings Commented Feb 1, 2017 at 18:59

1 Answer 1

0

This Should Work.

([a-zA-Z]+)

Input:

"abc,pqr(1),xyz"

Output:

abc
pqr
xyz

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

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

1 Comment

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

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.