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

+1 vote
1 answer 154 views
0 votes
1 answer 160 views
+3 votes
1 answer 714 views
+2 votes
2 answers 796 views
–1 vote
0 answers 125 views
+6 votes
2 answers 2,574 views
+1 vote
1 answer 95 views
0 votes
1 answer 120 views
+2 votes
1 answer 607 views
+1 vote
1 answer 816 views

Not a Member yet?

Ask to Folks Login

My Account
628 Folks are online
2 members and 626 guest online
Your feedback is highly appreciated