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:
C#
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"
Posted

You don't even need regular expressions for such a task, just using the String.Split[^] method is enough.
 
Share this answer
 
Comments
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:

(.*?)(xxx|$)


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.

Regards,

Manfred

P.S.: If anybody has a shorter variant please let me know. :)
 
Share this answer
 
Comments
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 http://msdn.microsoft.com/en-us/library/8yttk7sy.aspx[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