0

I have a string like:

Brief Exercise 1-1 Types of Businesses Brief Exercise 1-2 Forms of Organization Brief Exercise 1-3 Business Activities.

I want to break above string using regular expression so that it can be like:

Types of Businesses
Forms of Organization
Business Activities.

Please don't say that I can break it using 1-1, 1-2 and 1-3 because it will bring the word "Brief Exercise" in between the sentences. Later on I can have Exercise 1-1 or Problem 1-1 also. So I want some general Regular expression.

Any efficient regular expression for this scenario ?

4
  • 2
    Without knowing the rules by which you want to "break" the string, we can't suggest a solution, regex or otherwise. Commented Jan 5, 2012 at 9:54
  • According to above scenario i want to break using "Brief Exercise 1-(n)" where n is 1,2 3, and so on. Commented Jan 5, 2012 at 9:57
  • In that case, what have you tried? Is it always a 1-(n)? Or can it be a (n)-(m)? In either case, what range can n and m have? Commented Jan 5, 2012 at 10:01
  • n = [0-9] any number. It is not fixed. It can go up till 40 or 50. Commented Jan 5, 2012 at 10:02

3 Answers 3

3
var regex=new Regex(@"Brief (?:Exercise|Problem) \d+-\d+\s");
var result=string.Join("\n",regex.Split(x).Where(a=>!string.IsNullOrEmpty(a)));

The regex will match "Brief " followed by either "Exercise" or "Problem" (the ?: makes the group non capturing), followed by a space, then 1 or more digits then a "-", then one or more digits then a space.

The second statement uses the split function to split the string into an array and then regex to skip all the empty entries (otherwise the split would include the empty string at the begining, you could use Skip(1) instead of Where(a=>!string.IsNullOrEmpty(a)), and then finally uses string.Join to combine the array back into string with \n as the seperator.

You could use regex.Replace to convert directly to \n but you will end up with a \n at the begining that you would have to strip.

--EDIT---

if the fist number is always 1 and the second number is 1-50ish you could use the following regex to support 0-59

var regex=new Regex(@"Brief (?:Exercise|Problem) 1-\[1-5]?\d\s");
Sign up to request clarification or add additional context in comments.

2 Comments

Why Problem? Where do you see that?
@Oded From the question "Later on I can have Exercise 1-1 or Problem 1-1 also"
2

This regular expression will match on "Brief Exercise 1-" followed by a digit and an optional second digit:

@"Brief Exercise 1-\d\d?"

Update:

Since you might have "Problem" as well, an alternation between Exercise and Problem is also needed (using non capturing parenthesis):

@"Brief (?:Exercise|Problem) 1-\d\d?"

10 Comments

I think it is the easiest and efficient approach.
You need to escape the \d, either as \\d or put an @ infront of the string, see my answer. Also you need to match the trailing space!
I just need one clarification. If I have variable like marker = "Brief Exercise 1" then how can I put it in Regular expression ?
@fawad - It needs to be inserted into the string directly. using string.Format with format specifiers can do the trick.
I'm not good in regular expression. Can you please write the syntax ?
|
1

Why don't you do it the easy way? I mean, if the regular part is "Brief Exercise #-#" Replace it by some split character and then split the resulting string to obtain what you want.

If you do it otherwise you will always have to take care of special cases.

string pattern = "Brief Exercise \d+-\d+";
Regex reg = new Regex(patter);
string out = regex.replace(yourstring, "|");
string results[] = out.split("|");

3 Comments

Regex has a split function so you can combine your last two statements into one split function
Also you will need to escape the \d hence... either \\d or put an @ infront of the string definition.
Thanks, I didn't see your answer at the time. Learned something new ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.