Hey there! any question in your mind? Ask It Now!.

Popular Categories

I want to find different occurrences of a sub string within string using regex?

+3 votes
asked in Programming by neha (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. 

1 Answer

+1 vote
answered by swag31 (1,430 points)
selected by neha
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.

Related Questions

+3 votes
1 answer 911 views
–1 vote
0 answers 192 views
+6 votes
2 answers 3,551 views
+1 vote
1 answer 161 views
+2 votes
1 answer 1,283 views

Not a Member yet?

Ask to Folks Login

My Account
268 Folks are online
1 members and 267 guest online
Your feedback is highly appreciated