Click here to Skip to main content
15,946,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
string str ="my first statement xxx my second statement xxx my third statement";

how shall i write a regular expression to get three statements listed above based on searching strings between "xxx"

You don't even need regular expressions for such a task, just using the String.Split[^] method is enough.
Share this answer
Ankur\m/ 2-Jan-12 6:47am    
I second that unless OP is a big Regular Expression fan. ;)
Espen Harlinn 2-Jan-12 6:47am    
And it's a better solution, my 5
Manfred Rudolf Bihy 2-Jan-12 6:57am    
Better solution than using Regular Expressions in a trivial case such as this! 5+
arun_pk 2-Jan-12 7:05am    
I just simplified my problem and asked..
Actually my req is bit complex ..
If your case isn't more complex than stated in your question I'd advise you to go with CPallini's solution here[^].

Just in case you'd like to know anyhow how a regular expression would look like to achieve your goal:


This expression was verified to work with the free tool Regular Expression Designer[^].

It will give you four matches instead of three where the last match is the empty statement followed by the end of line. This is about the simplest regular expression I could come up with to achieve your goal.



P.S.: If anybody has a shorter variant please let me know. :)
Share this answer
arun_pk 2-Jan-12 7:11am    
Thanks Manfred.
I will use this..
and i will let u know if some probs in my req

My 5
Manfred Rudolf Bihy 2-Jan-12 7:29am    
Do try out the Regular Expression Designer I linked to in my solution. It's a great tool, free and quite helpfull.
Try the split[method].
Basically, something like string[] substrings = Regex.Split(str, "xxx"); should do.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900