+3 votes
206 views
in Programming by (560 points)

I am trying to do like this : 
String string = "hey hello chill not get hot";
String strMyPattern = "he";

Desired list = {"hey","hello"};

I want a fast way with the regex.

out put  should come with strMyPattern substrings. 

closed

1 Answer

+1 vote
by (1.5k points)
selected by
 
Best answer
hello , why don't you try this    he\w+
It matches all the words that start with he

you can also simply do this:

       String string="he";
       String regex = string+"\w+";

it (string) will give substrings starts with "he"
You can use

          string.matches(strMyPattern ) .

But you need to  create the list ,so you may use looping for testing.
In case of Commons Collections, you can use
   
          CollectionUtils.filter(list, predicate).

hope you get it.

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated