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
124 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 145 views
0 votes
1 answer 149 views
+3 votes
1 answer 643 views
+2 votes
2 answers 788 views
–1 vote
0 answers 111 views
+6 votes
2 answers 2,240 views
+1 vote
1 answer 85 views
0 votes
1 answer 115 views

Not a Member yet?

Ask to Folks Login

My Account
867 Folks are online
81 members and 786 guest online
Your feedback is highly appreciated