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
145 views
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 175 views
0 votes
1 answer 201 views
+3 votes
1 answer 811 views
+2 votes
2 answers 810 views
–1 vote
0 answers 153 views
+6 votes
2 answers 2,966 views
+1 vote
1 answer 123 views
0 votes
1 answer 142 views
+2 votes
1 answer 861 views
+1 vote
1 answer 912 views

Not a Member yet?

Ask to Folks Login

My Account
463 Folks are online
5 members and 458 guest online
Your feedback is highly appreciated